WPF DatePicker更改月份时引发异常

问题描述

| 好的,我有一个正在WPF(Visual C#2010 Express)中处理的项目,并且我有一些绑定到DateTime属性的DatePicker。现在,如果我只是打开“选择器”并立即开始更改月份,就没有问题了。但是,如果我选择一个日期然后尝试更改月份,则会收到以下异常:   元素不存在或存在   虚拟化使用VirtualizedItem   模式(如果支持)。 堆栈跟踪仅显示:   [外部代码]   我的方法打开包含DatePickers的对话框(以及其他方法)   [外部代码] 如果属性是否可为空,则似乎没有什么不同,并且在上述例外情况下,无法在任何主要搜索引擎上找到任何结果。 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 仔细阅读一下,如果这是您遇到的问题,它应该报告所有绑定错误。