windows-8 – datetimepicker绑定问题

前端之家收集整理的这篇文章主要介绍了windows-8 – datetimepicker绑定问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 @L_403_0@ 8.1的新dateTimePicker:
<DatePicker HorizontalAlignment="Left" Margin="401,245,0" Grid.Row="1"
 VerticalAlignment="Top" Width="352" Date="{Binding personSingle.personDOB,Mode=TwoWay}"/>

当我改变日期时,当我查看personDOB的值时,我没有得到我选择的值.
personDOB的类型为DateTimeOffset

我需要做什么才能获得我选择的价值?

更新:

<DatePicker x:Name="dtPick" HorizontalAlignment="Left" Margin="401,0" Grid.Row="1" 
VerticalAlignment="Top" Width="352" DataContext="{Binding personSingle}"
 Date="{Binding personSingle.personDOB.Date,Mode=TwoWay}"/>
我从这个链接找到了答案:

http://bretstateham.com/binding-to-the-new-xaml-datepicker-and-timepicker-controls-to-the-same-datetime-value/

您需要编写一个转换器才能使其正常工作:

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,string language)
    {
        try
        {
            DateTimeOffset dto = (DateTimeOffset)value;
            return dto.DateTime;
        }
        catch (Exception ex)
        {
            return DateTime.MinValue;
        }
    }
}
原文链接:https://www.f2er.com/windows/364698.html

猜你在找的Windows相关文章