RSS

Binding null-value of DateTime to DatePicker using an Attached Property

31 May

As you have seen in the post Binding null-value of DateTime to DatePicker  you could not bind a DateTime to a DatePicker without drawbacks. Because if you bind a non nullable DateTime, you will get after opening the DatePicker Popup the Calendar date range near the date 01.01.0001. To avoid that you can use the solution in the linked article or with my new solution using an AttachedProperty. With that one if the bound DateTime, which is not nullable, is DateTime.MinValue, the DatePicker shows on opening the Popup, on which you select a date, the actual date.

On the following picture you see left the situation if you bind a DateTime direct do a DatePicker, right the situation if you do it using my AttachedProperty.
image

<StackPanel Orientation"Horizontal" Height="30" VerticalAlignment="Top">
    <DatePicker Width="300" 
                SelectedDate="{Binding Path=SelectedDateTimeWhichIsNonNullable}" 
                WPFToolkit:CustomAttachedProperties.ShowNullWhenDateTimeMinValue="True">
    </DatePicker>
    <DatePicker Width="300" 
                SelectedDate="{Binding Path=SelectedDateTimeWhichIsNonNullable}">
    </DatePicker>
</StackPanel>

The logic for the AttachedProperty looks like:

public static class CustomAttachedProperties
{
    public static readonly DependencyProperty ShowNullWhenDateTimeMinValueProperty = 
                     DependencyProperty.RegisterAttached("ShowNullWhenDateTimeMinValue", 
                                        typeof (bool), 
                                        typeof (CustomAttachedProperties),
                                        new PropertyMetadata(default(bool),
                                        SetShowNullWhenDateTimeMinValueChanged));

    private static void SetShowNullWhenDateTimeMinValueChanged(DependencyObject d, 
                                           DependencyPropertyChangedEventArgs e)
    {
        DatePicker control = (DatePicker)d;
        if ((bool)e.NewValue)
        {
            control.SelectedDateChanged += OnSelectedDateChanged ;
        }
        else
        {
            control.SelectedDateChanged -= OnSelectedDateChanged ;
        }
    }

    public static void SetShowNullWhenDateTimeMinValue(UIElement element, bool value)
    {
        element.SetValue(ShowNullWhenDateTimeMinValueProperty, value);
    }

    private static void OnSelectedDateChanged (object sender, SelectionChangedEventArgs e)
    {
        UIElement control = (UIElement)sender;

        if ((bool)control.GetValue(ShowNullWhenDateTimeMinValueProperty))
        {
            var currentSelectedDate = ((DateTime?)control.GetValue(DatePicker.SelectedDateProperty));
            if (currentSelectedDate.HasValue && currentSelectedDate.Value == DateTime.MinValue)
            {
                //currently set DateTime.Now that the DatePicker opens the Popup with the current month
                control.SetValue(DatePicker.SelectedDateProperty, DateTime.Now);
                control.SetValue(DatePicker.SelectedDateProperty, null);
            }
        }
    }

    public static bool GetShowNullWhenDateTimeMinValue(UIElement element)
    {
        return (bool)element.GetValue(ShowNullWhenDateTimeMinValueProperty);
    }
}
Advertisements
 
3 Comments

Posted by on May 31, 2012 in C-Sharp, WPF

 

Tags:

3 responses to “Binding null-value of DateTime to DatePicker using an Attached Property

  1. Karin

    November 5, 2014 at 11:08

    Can yop please show how you set up the class CustomAttachedProperties

     
    • Michael Mairegger

      November 5, 2014 at 11:31

      If you create a class public static class CustomAttachedProperties { //add attached property here } then you are done. I have modified the code

       
      • Karin

        November 5, 2014 at 12:55

        Thank you 😀

         

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

Google+ photo

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

Connecting to %s

 
%d bloggers like this: