locked
Error Binding DatePicker control Date property

    Question

  • I am building a Universal App and am getting a run time error on my DatePicker control when trying to bind the Date property.

    I have this in my XAML:

     <DatePicker x:Uid="StartDate" Header="Start Date" Date="{Binding StartDateUtc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

    I am binding it to a DateTime property called StartDateUtc. However when firing up the app, I am getting this error:

    Exception = {"The text associated with this error code could not be found.\r\n\r\nFailed to assign to property '%0'. [Line: 42 Position: 63]"}

    Position 63 happens to be the Date Property. If I remove it, the control is displayed.

    Friday, November 21, 2014 12:22 PM

Answers

  • OK - fixed my issue. For those who have the same issue, set your property to parse a datetime.

            private DateTime _startDate = DateTime.Parse(DateTime.Today.ToString());
            public DateTime StartDate
            {
                get { return _startDate; }
                set { SetProperty(ref _startDate, value); }
            }

    In the XAML:

    <DatePicker Date="{Binding StartDate, Mode=TwoWay, Converter={StaticResource DateTimeToDateTimeOffset}}"/>

    ...and the Converter:

     public class DateTimeToDateTimeOffsetConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                try
                {
                    DateTime date = (DateTime)value;
                    return new DateTimeOffset(date);
                }
                catch (Exception ex)
                {
                    return DateTimeOffset.MinValue;
                }
            }

            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                try
                {
                    DateTimeOffset dto = (DateTimeOffset)value;
                    return dto.DateTime;
                }
                catch (Exception ex)
                {
                    return DateTime.MinValue;
                }
            }
        }

    Monday, February 02, 2015 12:27 PM

All replies

  • Hi Craig Muckleston,

    I assume you want to bind a C# object to control in XAML.cs. If so, I would suggest you create a custom class and then assign it to the static resources. Please see the following code snippet.

    <Page.Resources>
            <local:CustomClass  x:Key="cl1"></local:CustomClass>
        </Page.Resources>
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <StackPanel>
                <DatePicker x:Uid="StartDate" Header="Start Date" Date="{Binding StartDateUtc, Source={StaticResource cl1}}" />
            </StackPanel>
    </Grid>
    

    If this doesn’t work or I misunderstand you, please post more information about your scenario.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, November 24, 2014 7:14 AM
    Moderator
  • Hi Herro,

    All I'm trying to do is bind the Date property of the DatePicker to a property in my ViewModel

    Monday, November 24, 2014 11:48 AM
  • Hi Craig,

    Yes. Can you give me a sample project for reproduction? Use you OneDrive and share a link here.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Wednesday, November 26, 2014 7:09 AM
    Moderator
  • Hi Herro,

    I'm not sure a whole project is needed. Here's the XAML

    <DatePicker x:Uid="StartDate" Header="Start Date" Date="{Binding StartDateUtc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

    Here's the property in the ViewModel

    private DateTime _startDateUtc;
    [RestorableState]
    public DateTime StartDateUtc
    {
     get { return _startDateUtc; }
     set
     {
      SetProperty(ref _startDateUtc, value);
     }
    }

    Wednesday, November 26, 2014 3:41 PM
  • Hi Craig,

    >> I'm not sure a whole project is needed. Here's the XAML

    I failed to reproduce your issue “Failed to assign to property”.  Could you please share a sample project through OneDrive to help us reproduce this issue?

    By the way, the type of DatePicker.Date property is DateTimeOffset rather than DateTime. Please try the ViewModel as following and let me know if it makes any difference.

        public class CustomViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            private DateTimeOffset _startDateUtc;
    
            [RestorableState]
            public DateTimeOffset StartDateUtc
            {
                get
                {
                    return this._startDateUtc;
                }
    
                set
                {
                    if (value != this._startDateUtc)
                    {
                        this._startDateUtc = value;
    
                        NotifyPropertyChanged();
                    }
                }
            }
    
        }

    Regards,

    Jeffrey


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, December 04, 2014 6:20 AM
  • OK - fixed my issue. For those who have the same issue, set your property to parse a datetime.

            private DateTime _startDate = DateTime.Parse(DateTime.Today.ToString());
            public DateTime StartDate
            {
                get { return _startDate; }
                set { SetProperty(ref _startDate, value); }
            }

    In the XAML:

    <DatePicker Date="{Binding StartDate, Mode=TwoWay, Converter={StaticResource DateTimeToDateTimeOffset}}"/>

    ...and the Converter:

     public class DateTimeToDateTimeOffsetConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                try
                {
                    DateTime date = (DateTime)value;
                    return new DateTimeOffset(date);
                }
                catch (Exception ex)
                {
                    return DateTimeOffset.MinValue;
                }
            }

            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                try
                {
                    DateTimeOffset dto = (DateTimeOffset)value;
                    return dto.DateTime;
                }
                catch (Exception ex)
                {
                    return DateTime.MinValue;
                }
            }
        }

    Monday, February 02, 2015 12:27 PM