Datepicker databinding

2 posts, 0 answers
  1. Ivan
    Ivan avatar
    7 posts
    Member since:
    Nov 2008

    Posted 26 Jan 2009 Link to this post

    Hello!

    Currently i am working on an application wich needs two datepickers - a StartDate and an EndDate . I am having problems binding this two controls with DateTime properties on an object . For binding values i am using the attribute SelectedDate = {Binding Path=StartDate}, but no binding is created . I would like to know if this is the correct way of binding this control to date fields or if there is another way of doing this... I have search throught the website/documentation but I can't find a proper response.

    Any help would be great !
    Thanks,
    Ivan Frias.

  2. Miroslav Nedyalkov
    Admin
    Miroslav Nedyalkov avatar
    1718 posts

    Posted 27 Jan 2009 Link to this post

    Hello Ivan,

    What effect do you want to achieve? If you want one time binding this should be enough. If you want one way binding you should make your data object implement INotifyPropertyChanged interface and raise the PropertyChanged event when changing a property. If you want to create two way binding (if you want to make the DataPickers update the data object), you should mark the binding as TwoWay explicitly.
    This is an example (the second DatePicker is two way bound):
            <StackPanel> 
                <Telerik:RadDatePicker Width="150" HorizontalAlignment="Center" 
                        VerticalAlignment="Center" IsTodayHighlighted="True" 
                        SelectedDate="{Binding Path=StartDate}" /> 
     
                <Telerik:RadDatePicker Width="150" HorizontalAlignment="Center" 
                        VerticalAlignment="Center" IsTodayHighlighted="True" 
                        SelectedDate="{Binding Path=EndDate, Mode=TwoWay}" /> 
            </StackPanel> 
    And this is the class of the data object:
        public class Data : INotifyPropertyChanged 
        { 
            private DateTime startDate; 
     
            public DateTime StartDate 
            { 
                get 
                { 
                    return this.startDate; 
                } 
                set 
                { 
                    if (this.startDate != value) 
                    { 
                        this.startDate = value; 
                        OnPropertyChanged("StartDate"); 
                    } 
                } 
            } 
     
            private DateTime endDate; 
     
            public DateTime EndDate 
            { 
                get 
                { 
                    return this.endDate; 
                } 
                set 
                { 
                    if (this.endDate != value) 
                    { 
                        this.endDate = value; 
                        OnPropertyChanged("EndDate"); 
                    } 
                } 
            } 
     
            protected virtual void OnPropertyChanged(String propertyName) 
            { 
                if (System.String.IsNullOrEmpty(propertyName)) 
                { 
                    return
                } 
                if (PropertyChanged != null
                { 
                    PropertyChanged(thisnew PropertyChangedEventArgs(propertyName)); 
                } 
            } 
     
            public event PropertyChangedEventHandler PropertyChanged;         
        } 

    Hope that helps.

    All the best,
    Miroslav Nedyalkov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top