Jonathan Allen
Jonathan Allen

Reputation: 70307

WPF DataGrid: Can I cancel a change selection action?

Currently I'm catching the SelectionChanged event, but I would prefer to catch an eariler event that will allow me to cancel the selection change.

Background:

I have two data grids, the lower being a detail of the upper. When the upper changes, I currently prompt the user to save changes. But if there are validation errors, I want to offer them the option to cancel the selection change and fix those errors.

Upvotes: 4

Views: 1946

Answers (1)

Ε Г И І И О
Ε Г И І И О

Reputation: 12341

You can try binding the upper grid's ItemsSource an ICollectionView as follows.

var items = CollectionViewSource.GetDefaultView(*your current bound collection* );
items.CurrentChanging += this.OnCurrentItemChanging;
*your grid*.ItemsSource = items;

Then inside OnCurrentItemChanging, you can make e.Cancel = true which will cancel the selection change.

Upvotes: 4

Related Questions