Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid ItemsSource Binding Issue

I have a DataGrid in a WPF application that binds itself to an ObservableCollection of objects, and everything works fine. Now if I modify a cell in the datagrid during runtime, and delete the content, leave the cell empty. The observableCollection's corresponding value will not be modified, it will be the old value. But when I exit the window containing the datagrid and restart the window, it throws a XamlParseException, says:"Set Property 'System.Windows.Controls.ItemsControl.ItemsSource' threw an exception"

  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: 

It won't throw the exception as long as when I closed the window, no cell in the datagrid is empty. I also checked the Collection before the Window does InitializeComponent Line and it looks perfectly fine, all the objects have the correct value, no empty or nulls. I cannot figure out why it's throwing this exception. any ideas? Thanks in advance.

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}}">
like image 208
Ling Xing Avatar asked Mar 07 '11 19:03

Ling Xing


1 Answers

There appear to have been quite a few issues with the DataGrid & DeferRefresh. Here are two relevant posts:

The generic recommendation seems to be to examine the DataGrid's associated view as an IEditableCollectionView (which yours would be by default as ObservableCollection gets a ListCollectionView during binding when last I looked) and check IsAddingNew & IsEditingItem to see if it's safe to modify the view. Such modifications would include DeferRefresh, which is essentially a Disposable used to batch up modifications to the view.

As far as I can see, the problem you have is that the DataGrid is midway through a transaction, or the view is set to believe it is, whilst you are changing ItemsSource (which would change or entirely replace the associated bound view). That you are clearing out a cell but it is not yet modifying the underlying data implies it's begun an edit (BeginEdit), but not yet committed (CommitEdit), which occurs when you change cell by default.

That it is happening when you are closing & reopening the window implies that some preserved state is kept around that still thinks a transaction is in progress. Perhaps it is still using the same ListCollectionView as before, and that still has IsEditingItem set to true?

The fix is most likely to ensure that any and all edits are committed before you close the window.

like image 144
naviwhack Avatar answered Oct 01 '22 16:10

naviwhack