Cancel RecordChanging Event

Oct 7, 2008 at 2:22 PM
I have a grid bound to my business objects. When a user makes a change to record and then tries to navigate away from the record I need to do the following:

  1. Check to make sure the object is valid
  2. If it is valid then automatically save, otherwise force them to stay on the currently selected record.

When I'm not using a datagrid I have no problem doing this. I have a Record Navigator (VCR) control that I uses the CollectionView (CollectionViewSource.GetDefaultView) of my DataContext (ObservableCollection in this case) and handle the RecordChanging event. If the object isnot valid then I do not allow navigation to the next record. The problem is, that DataGrid does not honor this.

First of all, I'm assuming that the grid is binding to the collection with a view OTHER THAN the default view. I can tap into the RecordChanging event via myGrid.Items.RecordChanging, but setting CurrentChangingEventArgs.Cancel = True does not stop the grid from selecting a different row.

What is the proper way of handling this?


Oct 7, 2008 at 5:07 PM
Row validation will be in the final release which will solve your first problem.  For the second issue, it will be fixed in the final release.  The behavior will be such that editing stays on the current invalid record and you cannot edit any other records until it is corrected either by cancelling or changing it to the correct value(s).

As far as your assumption, it is using a CollectionView just like all other ItemsControls.  The type of view just depends on the type of data source you give it.  For example, if you set the ItemsSource to be a DataView then the default collection view will be a BindingListCollectionView.  By the way, is Record Navigator your own implemented collection view control?  I'm not sure what CurrentChangingEventArgs.Cancel is but in the DataGrid.CurrentCellChanging, you cannot cancel the operations like some other events on the DataGrid.
Dec 16, 2008 at 11:00 PM
Hello Vincent !
I have exactly the same problem of micahlmartin and I think It is a classic problem
I Need to validate my DataItem and I will go to use CurrentChanging Event of ICollectionView
I just begin try to change source code Of datagrid to do this (the last version of November) (for exemple add code in DataGridCell just before Focus calls in differents places in the code but I think I will go in a bad and very hard way ...

In fact, I think the problem exists on each control derivated from Selector Control, Nothing seems construct to be cancelable when I want change the current Item in CollectionView by UI component like List, ComboBox and .... DataGrid too !!!!

Example : a simple List control on simple List<MyItem> control, If I clic on second Item (the current is the first) and Return Cancel=True in CurrentChanging event of CollectionView of my List (obtains by GetDefaultViewSource) >>> Ok, the current item of CollectionView don't change BUT the selected Item of List is the second  item !!! List and CollectionView is desynchronized !!!!!

It is a very big problem for us ... I don't know how can I construct a Input Data Framework with this issue ... I Search ...

could you help us or give us a another way to work

thanks a lot

Bests regards

Dec 18, 2008 at 9:08 PM
As I've told micahlmartin, row validation (BindingGroup) may be able to help with this problem or is validation how you want it and it's the navigation that you are having problems with?
Jan 6, 2009 at 10:06 PM
Hello !

I am apologize for the delay, I Just see your answer ... I forgotten to check the EMail notification .. oups ....

In Fact, I build a Generic Input Framework for my company witch create Business application ...
And so, I develop, for example, a VCR control like micahlmartin, this control is "Binded" on current Datacontext, and so, If Curent DataContext is a Enumerable Object, I search associated ICollectionView component and call for example MoveCurrentToNext()

If I Do this, No problem occur when ICollectionView.CurrentChanging return false by CurrentChangingEventArgs.Cancel .... Of course, the CurrentItem does not change, and so all UI control is binded on the same item ...

BUT if CurrentItem of ICollectionView (no matter of the concret class under ICollectionView (BindingListCollectionView, ListCollectionView, etc ...) is changing by Selector control (ListBox or .... Datagrid) with IsSynchronizedWithCurrentItem=True
(For example, If A User clic on another Item in listBox), I have the problem wich explained in my first post !!!!!!

so, My problem is, I think, a navigation problem

The only way I found since my first post, is to change the code Of DataGrid to add a check of CurrentChanging Event of IcollectionView of my selector control ...
(I do nothing for ListBox ... I need search the good way ...)

I use this type of code (not very beautiful code .... :-(  and I am sure lot of user's scenarii is not cover ... It Is just an experimentation )

DataGrid.Cs .....

        private void MakeFullRowSelection(object dataItem, bool allowsExtendSelect, bool allowsMinimalSelect)
            if (!OKToChange(dataItem))

        public bool OKToChange(object newItem)
            if (this.CurrentCell.Item != newItem)
                return Isagri.Windows.Data.Internal.CollectionViewHelper
                return true;

Helper.cs :

        private static MethodInfo _miOKToChangeCurrent = null;
        private static PropertyInfo _piCollectionViewOnItemsCollection = null;
        static CollectionViewHelper()
            _miOKToChangeCurrent = typeof(CollectionView).GetMethod("OKToChangeCurrent", BindingFlags.NonPublic | BindingFlags.Instance);
            _piCollectionViewOnItemsCollection = typeof(System.Windows.Controls.ItemCollection).GetProperty("CollectionView", BindingFlags.NonPublic | BindingFlags.Instance);

        public static bool OKToChangeCurrent(CollectionView cv)
            if (cv is System.Windows.Controls.ItemCollection)
                cv = _piCollectionViewOnItemsCollection.GetValue(cv, null) as CollectionView;
            return (bool)_miOKToChangeCurrent.Invoke(cv, null);

Jun 20, 2009 at 10:21 AM


We have developed a derivated Datagrid On Last version of Datagrid WPF ToolKit. This new class includes the previous approach to turn around the ItemsControl CurrentChanging bug.

My question :

Are what the bug on the desynchronization of ItemsControls (when the event "CurrentChanging" of the current CollectionView responds negatively) will be fixed in WPF 4.0?

For us, It is very important ... If this bug will not be corrected, 2 possibility for us :

* we keep WPF ToolKit source in our internal tools framework (and keep all cost to maintain it during several years :-( ) or

* search to turn arround by another way (We know of no other solution to date)

Thanks a lot for your attention and your responses

Best regards

Jun 25, 2009 at 11:46 AM


I just do a test on CTP of Visual Studio 2010 ... with WPF 4.0

The bug always occurs ...