好的我有我在WPF(Visual C#2010 Express)中工作的这个项目,并且我有几个绑定到DateTime属性的DatePickers。现在,如果我打开“选择器”并立即开始更改月份,则不会有任何问题。但是,如果我选择一个日期,然后尝试更改月份,我得到以下异常:WPF 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的专家很远,有诚实不知道什么可能导致这或从哪里开始寻找..
尝试在迷你应用程序中重现。要么你发现问题,要么你在这里发布信息。 – 2011-05-26 11:05:37
好主意,不敢相信我甚至没有考虑过..但无论如何,我设法在一个全新的项目中用最少量的代码复制完全相同的错误。奇怪的是,如果我选择忽略异常并继续运行,它不会再次抛出,我可以改变月份就好了。 (也许VS记得我的选择继续?) XAML:https://gist.github.com/993005 C#:https://gist.github.com/993003 – Robin 2011-05-26 12:09:30
我把你的代码移到了实际问题中github今天去世了。 – user7116 2011-05-26 13:41:36