How do I set TargetNullValue to a date?
Asked Answered
Q

1

6

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?

Queer answered 5/2, 2009 at 17:56 Comment(0)
C
14

Try this:

xmlns:sys="clr-namespace:System;assembly=mscorlib"
...

TargetNullValue={x:Static sys:DateTime.Now}
Chesna answered 6/2, 2009 at 13:37 Comment(4)
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}".Queer
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.Queer
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.Bocock
How to do it in Silverlight? #8693362Banbury

© 2022 - 2024 — McMap. All rights reserved.