我正试图为一个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
是:
发布于 2013-10-31 15:13:05
您没有正确地绑定DataContext
。用这个:
<toolkit:DateTimePicker DataContext={Binding MyDateTime}" Value="{Binding ., Mode=TwoWay}" Loaded="dateTimePicker_Loaded" Format="SortableDateTime"/>
发布于 2013-10-31 15:11:30
您没有正确设置DataContext
。试一试
<toolkit:DateTimePicker DataContext="{Binding Path=MyDateTime}" Value="{Binding ,Mode=TwoWay}" Loaded="dateTimePicker_Loaded" Format="SortableDateTime"/>
https://stackoverflow.com/questions/19710127
复制相似问题