所有,「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。
在此先感謝。