This is a migrated thread and some comments may be shown as answers.

Default date value 01/01/001

5 Answers 451 Views
DateTimePicker
This is a migrated thread and some comments may be shown as answers.
Robin
Top achievements
Rank 1
Robin asked on 09 Sep 2010, 02:18 PM
Hi,

Im binding the RadDatePicker to a viewModel dateTime property. when the propert is null i get 01/01/001. How to i change this behaviour to just leave the date field blank. I thought about using the TargetNullValue property in binding but not sure what to target the value to? I tried 0 but that didnt work...?

<

 

 

telerik:RadDatePicker x:Name="rdActionDueDate" SelectedValue="{Binding actionDueDate, Mode=TwoWay, UpdateSourceTrigger=Explicit,TargetNullValue=0}" />

 

5 Answers, 1 is accepted

Sort by
0
Miroslav Nedyalkov
Telerik team
answered on 09 Sep 2010, 02:41 PM
Hi Chris,

 The type of the SelectedValue property of the DateTimePicker control is DateTime? which means it can have null value. If the corresponding property of your ViewModel is of type DateTime, this causes the problem. You can fix it either by changing the type of the property of your ViewModel or by providing a value converter to the binding where to handle the situation.

If you have further questions, don't hesitate to ask!

Best wishes,
Miroslav Nedyalkov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Robin
Top achievements
Rank 1
answered on 09 Sep 2010, 04:33 PM
Hi there. thanks for the quick reply.

Happy to change out the property but not sure what I should be changing this too? Do you have any further information you could provide on this.

Thanks in advance
Chris
0
Miroslav Nedyalkov
Telerik team
answered on 10 Sep 2010, 08:57 AM
Hi Chris,

 What you need to do is to change the type of the actionDueDate property from DateTime to DateTime? as the type of the SelectedValue of the RadDateTimePicker control is DateTime?.

Let us know if you have further questions.

Sincerely yours,
Miroslav Nedyalkov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Robin
Top achievements
Rank 1
answered on 10 Sep 2010, 10:13 AM
Hi There,

That worked, thanks very much for your help :)

Chris
0
David
Top achievements
Rank 1
answered on 18 Feb 2014, 03:00 PM
I'm figuring out how to deal with the converter. I don't want that for any reason there appears a 1/1/0001 nor 1/1/0001 12:00 AM 

In case you cant change the ViewModel's type you can try this converter:


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;
    }
 }
Tags
DateTimePicker
Asked by
Robin
Top achievements
Rank 1
Answers by
Miroslav Nedyalkov
Telerik team
Robin
Top achievements
Rank 1
David
Top achievements
Rank 1
Share this question
or