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
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!
Miroslav Nedyalkov
the Telerik team
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
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.
Miroslav Nedyalkov
the Telerik team
That worked, thanks very much for your help :)
Chris
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;
}
}