我正在尝试使用
Windows 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}"/>
我从这个链接找到了答案:
原文链接:https://www.f2er.com/windows/364698.html您需要编写一个转换器才能使其正常工作:
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; } } }