Default date value 01/01/001

6 posts, 0 answers
  1. Robin
    Robin avatar
    10 posts
    Member since:
    Jun 2009

    Posted 09 Sep 2010 Link to this post

    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}" />

     

  2. Miroslav Nedyalkov
    Admin
    Miroslav Nedyalkov avatar
    1718 posts

    Posted 09 Sep 2010 Link to this post

    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
  3. DevCraft banner
  4. Robin
    Robin avatar
    10 posts
    Member since:
    Jun 2009

    Posted 09 Sep 2010 Link to this post

    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
  5. Miroslav Nedyalkov
    Admin
    Miroslav Nedyalkov avatar
    1718 posts

    Posted 10 Sep 2010 Link to this post

    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
  6. Robin
    Robin avatar
    10 posts
    Member since:
    Jun 2009

    Posted 10 Sep 2010 Link to this post

    Hi There,

    That worked, thanks very much for your help :)

    Chris
  7. David
    David avatar
    15 posts
    Member since:
    Nov 2011

    Posted 18 Feb 2014 in reply to Miroslav Nedyalkov Link to this post

    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;
        }
     }
Back to Top
DevCraft banner