0

I'm binding the RadDatePicker to a viewModel dateTime property. when the property is null in database i get 01/01/0001. How to i change this behavior to just leave the date field blank.. example:

In database ===> experiation_date : NULL In View(GUI) ===> experation_date : 01/01/0001

Expected Result ===> experation_date : {Blank}

3 Answers 3

1

Set the SelectedDate to null (and not DBNull):

RadDatePicker1.SelectedDate = null;

and in xaml set DateTimeWatermarkContent to the empty string:

<telerik:RadDatePicker x:Name="RadDatePicker1" DateTimeWatermarkContent="" />
2
  • How can i solve problem for < telerik:DataFormDataField DataMemberBinding="{Binding ExpirationDate> im facing same problem for this
    – Chetan S
    Apr 18, 2012 at 11:56
  • Yes i got solution for this problem...we just need to change datatype of that property which is bind for <telerik:DataFormDataField> control in view model.
    – Chetan S
    Apr 19, 2012 at 18:19
1

It is not always possible to change the Data Type from DateTime to DateTime? as a DateTime object might in fact be required. (A typical scenario is when your control is bound to a database object with a DateTime type but you are busy creating a new record in your control and so the value is still null at that point.) In such a case, it is possible to write a Value Converter as Paulo does in the following link: http://www.telerik.com/forums/default-date-value-01-01-001 (See his code for the EditDateConverter. It works just as-is...) I am pasting his code here for reference and in case that link stops working in future:

public class EditDateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null && value.GetType().FullName == "System.DateTime")
        {
            var dt2 = (DateTime)value;
            if (dt2 != DateTime.MinValue)
                return value;
        }
        DateTime dt;
        if (!DateTime.TryParse(value as string, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out dt))
            return dt != DateTime.MinValue ? value : null;
        return dt;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null && targetType.FullName == "System.DateTime")
            return DateTime.MinValue;
        return value;
    }
}

To use it in a binding for your RadDatePicker, simply add the converter as a resource in your XAML and then add the converter to your binding.

In your XAML namespace declarations you have to add the namespace of your converter, for example:

xmlns:converters="clr-namespace:MyNamespace.Converters"

And then in the Resources section of your XAML you would have:

<UserControl.Resources>
    <converters:EditDateConverter x:Name="NullableDate"/>
</UserControl.Resources>

Then you can simply add the following line to your binding on the control:

Converter={StaticResource NullableDate}

If you now have a null date the control will display the watermark (e.g. "Enter Date") rather than 01/01/0001.

0

If you are bind not nullable datetime variable i.e (DateTime) to control then it will use default 01/01/001 date instead of Empty.... So if you want the default value as a Empty then just change the datatype of your variable which is bound to control to (DateTime?).....

DateTime ===> DateTime?

It works fine...

Thank you.!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.