Home > C#, Troubleshooting, WPF > WPF DatePicker not updating SelectedDate Property

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.