【问题标题】:uwp CalendarDatePicker not binding to Date Property [duplicate]uwp CalendarDatePicker 未绑定到日期属性 [重复]
【发布时间】:2018-07-31 03:17:24
【问题描述】:

我正在尝试将 ViewModel 中的 DateTime 绑定到 calendarDatePicker,但它不起作用。我试过textbox 并且可以工作。

这部分有效:

<TextBox Text="{Binding MyDate}" ></TextBox>

这不是:

<CalendarDatePicker Date="{Binding MyDate}" />

【问题讨论】:

    标签: xaml uwp uwp-xaml


    【解决方案1】:

    这个问题之前回答过here on MSDN

    根据文章,您可以使用Converter 将绑定转换为DateTimeOffset。转换器可能类似于以下内容,来自this post(由 MSDN 回答引用):

    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;
            }
        }
    }
    

    【讨论】:

    • Works, Date ="{Binding MyDate, Mode=TwoWay ,Converter={StaticResource DateTimeToDateTimeOffset}}"
    猜你喜欢
    • 1970-01-01
    • 2017-11-08
    • 2017-06-17
    • 1970-01-01
    • 2018-05-21
    • 2018-09-03
    • 2016-03-15
    • 2016-02-22
    • 1970-01-01
    相关资源
    最近更新 更多