views:

1104

answers:

2

I'm using the WPF toolkit's Calendar control to allow users to select a date. If the date is not yet selected then the property the SelectedDate is bound to is Null. This makes the Calendar default you January 1, 0 AD. I'd like to do something like

SelectedDate="{Binding UserPickedDate, TargetNullValue=Today, Mode=TwoWay}"

But both "Today" and "Now" throw binding errors. Can I use TargetNullValue to set the default date to Today or Now?

+3  A: 

Try `{x:Static DateTime.Now}'

rudigrobler
Thanks, I was definitely having a brain freeze there. For others finding this you need to import mscorlib, e.g. xmlns:sys="clr-namespace:System;assembly=mscorlib". Then the binding is SelectedDate="{Binding UserPickedDate, TargetNullValue={x:Static sys:DateTime.Today}, Mode=TwoWay}".
Bryan Anderson
Setting TargetNullValue to Today in the Calendar control does have the unfortunate consequence that anytime you select Today after that the bound property is set to Null. For this reason my final code set TargetNullValue to Now which doesn't have this problem.
Bryan Anderson
This was what I was searching for. My date picker was remembering the last value after bound it to a new object that didn't have the date set yet.
Kevin
A: 

Hi,

What about SL4?

TargetNullValue={x:Static sys:DateTime.Today} doesn't work..