Access ItemsSource in DataGrid RowValidation

Nov 7, 2008 at 2:09 AM
I am trying to wire up a RowValidationRule that prevents duplicate rows from being created. Row equality is based on the values of two columns. Ideally I could access the collection bound to the DataGrid in the ValidationRule. 

Any ideas?

Nov 13, 2008 at 6:02 PM
You can create a custom validation rule with a property that points to the DataGrid.

class MyValidationRule : ValidationRule


                                public ItemsControl Host { get; set; }


                                public override ValidationResult Validate(…)


                                                IEnumerable myCollection = Host.ItemsSource;

                                                … check for duplicates…




You’ll have to set the Host property manually:

                myDataGrid.ValidationRules[3].Host = myDataGrid;

Nov 15, 2008 at 10:59 PM

Thanks Vinsibal!