3
votes

I have a wpf application in which I had this property to bind to a datepicker

public Nullable<System.DateTime> dpc_date_engagement { get; set; }

So I add a converter :

 public class DateConverter : IValueConverter
   {
       public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null)
            return ((DateTime)value).ToShortDateString();
        return String.Empty;
    }

       public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
       {
           string strValue = value.ToString();
           DateTime resultDateTime;
           return DateTime.TryParse(strValue, out resultDateTime) ? resultDateTime : value;
       }
   }

In XAML file :

                     <DatePicker >
                                <DatePicker.Text>
                                    <Binding Path="dpc_date_engagement" UpdateSourceTrigger="PropertyChanged">
                                        <Binding.Converter>
                                            <converter:DateConverter/>
                                        </Binding.Converter>
                                    </Binding>
                                </DatePicker.Text>
                            </DatePicker>

The problem is when the date is null, the displayed text is 1/1/0001.

  • How can I fix my code to display an empty string instead of 01/01/0001, for null values?
2
What about breakpoint in converter? - Yuri Dorokhov
@YuriDorokhov For null values ,it gives me value = 01/01/0001 00:00:00 in the method convert - Lamloumi Afif
have you tried setting a style that sets text to "" when value is null ? - Muds
have a data trigger on value x:null and set text to "".. should work . I haven't tried though. lemme know if you implement - Muds
@LamloumiAfif Obviously your Nullable<DataTime> does not hold a null value, but DateTime.MinValue (or a default-constructed DateTime) instead. If it would hold a null value, the cast expression (DateTime)value would throw on exception. - Clemens

2 Answers

8
votes

The easiest way I've been found to handle nullable DateTime field using in a DatePicker is setting TargetNullValue=''

In XAML file:

<DatePicker Text={Binding dpc_date_engagement, Mode=TwoWay, TargetNullValue='', UpdateSourceTrigger=PropertyChanged} />
5
votes

The Nullable value passed to your converter is not itself null, even if it holds a null value (it can't even be null, because it is a struct and therefore not nullable).

So instead of comparing value to null, you'll have to cast it to Nullable<Datetime> and then check its HasValue property.

Moreover, you seem to have something like DateTime.MinValue in your bound property instead of null. So you should check against that, too:

public object Convert(...)
{
    var nullable = (Nullable<DateTime>)value;

    if (nullable.HasValue && nullable.Value > DateTime.MinValue)
    {
        return nullable.Value.ToShortDateString();
    }

    return String.Empty;
}