所有,“System.InvalidOperationException”类型的第一次机会异常出现在PresentationFramework.dll
我使用的数据库第一实体框架V4.4。在DB(和数据模型)中,Table1与Table2具有1:多关系。我将WPF中的DataGrid绑定到Table1.Local.First()。Table2(为简单起见,假设Table1.Local中有一个实体开始)。
视图模型:
Public SomeEntityDBContextWithTable1AndTable2 Container { get; set; }
Public ICollection Table2ToDisplay { get { return Container.Table1.Local.First().Table2; } } //Note: :Many navigation properties return ICollection<T>, but the object type is of ObservableCollection<T>.
在XAML中,我有以下
<GroupBox Header=Table2 DataContext="{Binding Path=Table2ToDisplay, UpdateSourceTrigger=PropertyChanged}">
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserAddRows="True">
<DataGrid.Columns>
<!--A bunch of columns-->
</DataGrid.Columns>
</DataGrid>
</GroupBox>
当发生这种情况是一个文本框里面NewItemPlaceHolder点击,我得到PresentationFramework的System.InvalidOperationException发生的历史。 DLL。这不会使我的应用程序崩溃,但我在输出中看到它。我的猜测是实体被添加到另一个线程,因此CollectionChanged事件在另一个线程上触发,并且这导致InvalidOperationException。但是,由于代码主要是通过XML完成的,我似乎无法找到处理此异常的方法(或者它已经被处理,只是将它报告给输出)。有没有一种安全的方式使用CanUserAddRows =“True”与EntityFramework其中“:许多”导航属性的类型为ObservableCollection?
我应该指出,我也尝试在CollectionViewSource中包装我的Table2ToDisplay属性,但仍然在输出中看到InvalidOperationException。
在此先感谢。