我正在尝试为 a 创建一个 Loaded 事件DateTimePicker
,它将从用作其上下文的DateTime
对象中获取值,然后将其转换并设置回来。DateTimePicker
DateTime
代码:
private void dateTimePicker_Loaded(object sender, RoutedEventArgs e) {
DateTimePicker dateTimePicker = (DateTimePicker)sender;
DateTime dateTime = (DateTime)dateTimePicker.DataContext;
}
XAML:
<toolkit:DateTimePicker DataContext="MyDateTime" Value="{Binding Path=MyDateTime, Mode=TwoWay}" Loaded="dateTimePicker_Loaded" Format="SortableDateTime"/>
我在代码的第二行崩溃了。未处理的异常:指定的强制转换无效。是DataContext
: