简体   繁体   中英

WPF DataGrid Sorting Throws exception on Item adding

In my project I have default WPF DataGrid with ItemsSource bound to ObservableCollection of business-objects from ViewModel. This collection is pretty frequently updated (items might be added, removed and updated, all objects do implement INotifyPropertyChanged ) and everything works fine.

Users are allowed to change sorting in DataGrid by clicking column headers. This part works too (ALMOST always).

The propblem occures when two events happen at the same time (Collection modification and dataGrid sorting) and exception is thrown, stating that 'Sorting' can't be performed while 'NewItem' or 'EditItem' is in progress (Not exact message, original message is not in english and I could not find english version).

Also we could not reproduce the problem in debuggable environment, but we have crash-reports of it.

I understand what is the root of the problem and what's kind of wrong. But I still can't figure out what should I (can I) do to fix it. I can not manually commit CollectionView changes and/or delay sorting, since it's somewhere inside DataGrid . I can not control user sort Enabling since it would cause inconvinience for users. I can not even try-catch this exception because the action that throws comes from windows message pumping (user action) and goes straight to framework code.

Should I maybe create CollectionView inside ViewModel, control it and bind datagrid to it, but won't datagrid still have same problem with it's internal view? Maybe there are some common practices or bugs, that I could not find about.

I can provide some code if needed, but everything seems common and straight-forward.

This blog post gives some suggestions on your problem.

'Sorting' is not allowed during an AddNew or EditItem transaction' http://tliangnet.blogspot.com/2015/08/sorting-is-not-allowed-during-addnew-or.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM