Binding DatePicker SelectedValue to Nullable Date

Nov 24, 2008 at 3:14 PM
Should I be able to bind a DatePicker to a Nullable Date property of a class?

Such as:

Public Class Person
    Private _BirthDate As Date?

    Public Property BirthDate() As Date?
            Return _BirthDate    
        End Get 
        Set(ByVal value As Date?) 
            _BirthDate = value
        End Set
    End Property
End Class


<my:DatePicker SelectedDate="{Binding Path='BirthDate'}" />
At runtime, after changing the date in the DatePicker, a breakpoint in Birthday's Set code shows value = Nothing.
Nov 25, 2008 at 3:32 AM
Well, the DateTime for SelectedDate in DatePicker is Nullable so a null value should be fine. As for your your binding issue, try making the BindingMode explicitly TwoWay in your XAML with
<my:DatePicker SelectedDate="{Binding Path=BirthDate, Mode =TwoWay}" />

Does this fix your issue?

-Eric Fabricant, MSFT
Nov 26, 2008 at 6:48 PM
Thank you very much!!

I tried a simple example project based on the sample code above, without two-way binding, and noticed that after setting a date, the Set method was not called, but in my larger project, Birthdate's Set was being called without two-way binding.  The call was actually coming from my "SetProperties" procedure that updates the object based on the values in it's bound Clone when I hit the "Save" button on the control.  Neither the SetProperties procedure or Save button, or the fact that I actually bind a MemberwiseClone of the object and later update all of it's properties were included in the example.

So without Two-Way Binding, the date was changed on the UI side, without affecting the bound object. With Two-Way Binding, changing the date Does affect the bound object.  Either way, all of the writable properties were being changed by my SetProperties procedure that didn't make its way over to my example.

Sorry and Thanks,
Patrick Halisey