WPF DatePicker not updating SelectedDate Property
I ran into some issues with the WPF Date Picker where typing in a date into the text box was not updating the underlying property when clicking on another function. In my specific instance it had to do with focus and interop issues.
To get around this I changed my object binding from the SelectedDate Property to the Text property of the control. This now allows me to validate the date as the user types as well as updates the underlying object immediately so when focus is changed I don’t have to worry about whether or not the bindings were applied in time.
Before
<DatePicker Name="MyDatePicker" SelectedDate="{Binding Path=ActualDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
After
<DatePicker Name="MyDatePicker" Text="{Binding Path=ActualDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, TargetNullValue=''}" />
Advertisement
Categories: C#, Troubleshooting, WPF
binding, DatePicker, property, SelectedDate, updated, WPF