2011-03-07 255 views
7

我有一個WPF應用程序中的DataGrid,它自己綁定到一個ObservableCollection對象,並且一切正常。 現在,如果我在運行時修改數據網格中的單元格並刪除內容,請將單元格留空。 observableCollection的相應值不會被修改,它會是舊值。但是,當我退出包含DataGrid中的窗口,並重新啓動窗口,它拋出一個XamlParseException,說:「設置屬性‘System.Windows.Controls.ItemsControl.ItemsSource’拋出一個異常」WPF DataGrid ItemsSource綁定問題

StackTrace: 
     at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri) 
     at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) 
     at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) 
     at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) 
     at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) 
     at VO3.Start.InitializeComponent() in c:\VO\Trunk\VO3\Start.xaml:line 1 
     at VO3.Start..ctor() in C:\VO\Trunk\VO3\Start.xaml.cs:line 103 
    InnerException: System.InvalidOperationException 
     Message='DeferRefresh' is not allowed during an AddNew or EditItem transaction. 
     Source=PresentationFramework 
     StackTrace: 
      at System.Windows.Data.CollectionView.DeferRefresh() 
      at System.Windows.Controls.ItemCollection.SetCollectionView(CollectionView view) 
      at System.Windows.Controls.ItemCollection.SetItemsSource(IEnumerable value) 
      at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
      at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) 
      at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) 
      at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args) 
      at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType) 
      at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal) 
      at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) 
      at System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value) 
      at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value) 
      at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value) 
     InnerException: 

它不會拋出異常,只要當我關閉窗口時,datagrid中的單元格就不會爲空。我還在窗口初始化組件行前檢查了集合,它看起來非常好,所有對象都有正確的值,沒有空或空值。我無法弄清楚它爲什麼拋出這個異常。有任何想法嗎? 在此先感謝。

XAML:

<DataGrid HeadersVisibility="All" RowHeight="19" AutoGenerateColumns="False" Grid.Row="1" CanUserResizeColumns="False" 
           CanUserAddRows="False" CanUserDeleteRows="False" Block.TextAlignment="Right" Grid.RowSpan="2" CanUserReorderColumns="False" 
           ItemsSource="{Binding Collection}" Width="132" HorizontalAlignment="Right" Margin="10,0,10,0" CanUserSortColumns="False" 
           IsEnabled="{Binding ElementName=CheckBox, Path=SelectedIndex, Converter={StaticResource startEnabledConverter}}"> 
+0

集合中項目的屬性是否可以爲空?何時更新屬性(例如PropertyChanged,LoseFocus等)。 – CodeWarrior 2011-03-08 00:15:22

+0

集合中對象的屬性不可爲空,它們在propertychanged上更新。但從過去的經驗來看,當字段留空並不意味着集合中的相應對象爲空。並且在值被留空後我檢查了集合中的對象仍然是舊值。在窗口再次打開之前,我再次檢查了這些值,並且沒有空的屬性。出於某種原因,當WPF在DataGrid的ItemsSource DependencyProperty的set屬性中引發異常時 – 2011-03-08 13:40:56

+0

我建議您查找綁定到相同ObservableCollection的任何其他UI部分,並刪除對該集合的所有綁定以便測試你以上問題再次孤立。如果幸運的話,這只是其他控件使用的相應CollectionView的一個問題。 – 2011-11-29 10:26:46

回答

3

目前似乎已經與DataGrid & DeferRefresh不少問題。這裏有兩個relevantposts

通用的建議似乎是檢查DataGrid的相關觀點作爲IEditableCollectionView(其中你將默認爲的ObservableCollection,當我最後一次看着結合過程中得到了的ListCollectionView),並檢查IsAddingNew & IsEditingItem到看看是否可以修改視圖。這樣的修改將包括DeferRefresh,它本質上是一次性的,用於批量修改視圖。

據我所見,你遇到的問題是DataGrid是通過事務的中途,或者視圖被設置爲相信它,而你正在改變ItemsSource(這將改變或完全替換相關的邊界視圖)。您正在清除單元格,但尚未修改底層數據意味着它已開始編輯(BeginEdit),但尚未提交(CommitEdit),默認情況下會更改單元格時發生此問題。

當您正在關閉時發生這種情況&重新打開窗口意味着保留一些保留的狀態,但仍認爲事務正在進行。也許它仍然使用與之前相同的ListCollectionView,並且仍然將IsEditingItem設置爲true?

此修復程序最有可能確保在關閉窗口之前進行任何和所有編輯。

+0

這是正確的。最好的解決方案是使用此行爲:http://stackoverflow.com/a/6467171/724944 – surfen 2011-12-18 03:42:45

0

如果您正在消隱出來的字段是字符串類型沒有,那麼它可能無法正確更新。例如,如果它是一個「int」,並且將空白的單元格視圖模型的「int」屬性將不會被設置。

我已經解決了這個問題的方法是將轉換器的空白值轉換爲零的轉換器。

希望這有助於