0

我正在尝试为 a 创建一个 Loaded 事件DateTimePicker,它将从用作其上下文的DateTime对象中获取值,然后将其转换并设置回来。DateTimePickerDateTime

代码:

    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

4

2 回答 2

1

你没有DataContext正确设置。尝试

<toolkit:DateTimePicker DataContext="{Binding Path=MyDateTime}" Value="{Binding ,Mode=TwoWay}" Loaded="dateTimePicker_Loaded" Format="SortableDateTime"/>
于 2013-10-31T15:11:30.517 回答
0

您没有DataContext正确绑定。用这个:

<toolkit:DateTimePicker DataContext={Binding MyDateTime}" Value="{Binding ., Mode=TwoWay}" Loaded="dateTimePicker_Loaded" Format="SortableDateTime"/>
于 2013-10-31T15:13:05.850 回答