好的,我有我正在WPF(Visual C#2010 Express)中处理的项目,并且我有一些绑定(bind)到DateTime属性的DatePicker。现在,如果我只是打开“选择器”并立即开始更改月份,就没有问题了。但是,如果我选择一个日期然后尝试更改月份,则会收到以下异常:



堆栈跟踪仅显示:



这些属性是否可以为null似乎没有什么不同,在上述异常(exception)情况下,在任何主要搜索引擎上都找不到单个结果。

XAML

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <DatePicker SelectedDate="{Binding TheDate}"
                    x:Name="datePicker1" />
        <Button Content="Button"
                x:Name="button1"
                Click="button1_Click" />
    </Grid>
</Window>

后面的代码
    class TheClass
    {
        public DateTime TheDate { get; set; }
    }

    public MainWindow()
    {
        theClass = new TheClass();
        theClass.TheDate = DateTime.Now;

        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = theClass;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.MessageBox.Show(theClass.TheDate.ToString());
    }

是否有人以前曾见过这种行为或有其他想法?我距离WPF专家还很远,并且说实话也不知道是什么原因或从哪里开始寻找的。

最佳答案

我希望这可以是评论,而不是答案,但是这里有:

http://www.switchonthecode.com/tutorials/wpf-snippet-detecting-binding-errors

仔细阅读一下,如果这是您遇到的问题,它应该报告所有绑定(bind)错误。

关于c# - WPF DatePicker更改月份时引发异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6136966/

10-13 07:45
查看更多