DataGrid - Validation of entry against whole collection?

Mar 31, 2009 at 10:10 AM
I have come upon a situation where I need to validate a record that the user enters (or updates) in the DataGrid against all rows in the underlying collection. Basically, this is duplicates detection with a custom definition of what constitutes a duplicate. (There are a few distinct cases that need to be accounted for.) At first, I reached for RowValidationRules, but those have no concept of the collection to which the row is about to be added, which is an absolute requirement in this case, and they also have no means of passing parameters in a manner similar to that of value converters (so I cannot use a hack based around a RelativeSource binding either).

Currently, I am catching the "duplicate entries" condition when the user saves the changes in the UI ("save" is a distinct operation in the UI, so this works), but since we might be talking about hundreds of rows, I really would like to provide the user with some sort of immediate feedback when they add a duplicate row based on this custom logic's definition of duplicate. I know of MultiValueBindings, but they obviously don't help here, and I would really rather not couple the validation logic _too_ closely to the DataGrid in the presentation layer. The collection itself is an ObservableCollection<T> derivate, and for a moment, I thought of the CollectionChanged event, but there doesn't seem to be any way to override the collection change from there (NotifyCollectionChangedEventArgs.Action is read-only, and there is nothing like NotifyCollectionChangedEventArgs.Cancel).

Any suggestions would be greatly appreciated.
Coordinator
Mar 31, 2009 at 1:33 PM
This article describes a technique for global validation, http://www.codeproject.com/KB/WPF/GlobalWPFValidation.aspx.  It does stray away from the normal platform validation but it may give you some ideas.