开发者

WPF DateTimePicker not setting date after validation

开发者 https://www.devze.com 2023-02-15 22:30 出处:网络
I\'m trying to validate the selected date in a datetime picker control and setting it to today\'s date if the date selected is > Datetime.Today.The issue I\'m facing is that I\'m not able to set the S

I'm trying to validate the selected date in a datetime picker control and setting it to today's date if the date selected is > Datetime.Today.The issue I'm facing is that I'm not able to set the SelectedDate property of a datetimepicker control via xaml.I feel something is wrong with my binding, please can开发者_如何学Go you help?

Following is the code.Please can you tell me what 'am I doing wrong?

  <Controls:DatePicker Height="20"
                                 Grid.Row="0"
                                 Grid.Column="0"
                                 Grid.ColumnSpan="2"
                                 x:Name="dateControl"
                                 IsTodayHighlighted="True"
                                 Margin="5,10,5,20"
                                 SelectedDate="{Binding Path=BindingDate, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>

public class Context : INotifyPropertyChanged
    {
        public Context() { }

        private DateTime bindingDate = DateTime.Today;
        public DateTime BindingDate
        {
            get
            {
                return bindingDate;
            }
            set
            {
                if (DateTime.Compare(DateTime.Today, value) < 0)
                {
                    MessageBox.Show("Please Select Today date or older, Should not select future date");

//This is not reflected anytime in SelectedDate property of the control, why??? value = DateTime.Today; } bindingDate = value; OnPropertyChanged("BindingDate"); } }

..and yes I'm setting the datacontext of the window like the following:

public Window1()
        {
            InitializeComponent();
            this.DataContext = new Context(); 
        }

Any suggestions would be highly appreciated.

Thanks, -Mike


That is because the BindingDate setter will never be called if you set value for your local variable bindingDate and your ui will never be notified.

Instead of setting

private DateTime bindingDate = DateTime.Today.AddDays(13);

try setting

 BindingDate = DateTime.Today.AddDays(13);

EDIT

But selecting a future date in the datepicker will remain even after showing the messagebox because the selection is already made in the control and will not reset back.

But you can consider other alternatives like blocking all future dates from selection by using the BlackoutDates or DisplayDates property of the datepicker or you can conside using custom validation rules as mentioned in the below post

Date picker validation WPF


You could consider implementing INotifyPropertyChanging also, and not only INotifyPropertyChanged. In that way you can alsol notify that your property is about to change, and run some code accordingly. And of course notify that your property has effectively changed.

0

精彩评论

暂无评论...
验证码 换一张
取 消