WPF DatePicker更改月份时引发异常 [英] WPF DatePicker throws exception when changing month
问题描述
好吧,我有一个正在WPF(Visual C#2010 Express)中处理的项目,并且有一些绑定到DateTime属性的DatePicker。现在,如果我只是打开选择器并立即开始更改月份,就没有问题了。但是,如果我选择一个日期然后尝试更改月份,则会出现以下异常:
Ok I have this project I'm working on in WPF (Visual C# 2010 Express), and I have a few DatePickers that are bound to DateTime-properties. Now if I just open up the "picker" and start changing months immediately there is no problem. However, if I select a date and then try to change the month, I get the following Exception:
元素不存在或存在
已虚拟化;如果支持,则使用VirtualizedItem
模式。
Element does not exist or it is virtualized; use VirtualizedItem Pattern if it is supported.
堆栈跟踪仅显示:
[外部代码]
我的方法打开包含DatePickers的对话框(除其他外)
[外部代码]
[External Code]
my method that opens the dialog which contains the DatePickers (amongst other things)
[External Code]
如果属性为可空值,似乎没有什么不同,并且找不到一个结果
If the properties are nullable or not doesn't seem to make a difference, and can't find a single result on any of the major search engines on the above 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专家还很遥远,并且说实话也不知道是什么原因或从哪里开始寻找。.
Anyone ever seen this behaviour before or have any other ideas? I'm far from a WPF-expert and have honestly no idea what might be causing this or where to start looking..
推荐答案
我希望这可能只是评论而不是答案,但可以这样:
I wish this could be a comment and not an answer but here goes:
http://www.switchonthecode.com/tutorials/wpf-snippet-detecting-binding-errors
给出读段,如果这是您遇到的问题,它应该报告任何绑定错误。
Give that a read and it should report any binding errors if that is the issue in your case.
这篇关于WPF DatePicker更改月份时引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!