0

Suppose I have a object ListCollectionView and bind it to a datagrid. I bind the object's Name property to a TextBox(one of the datagrid's column)'s Text property and add my specific ValidationRule.

Now I want to the rule to check if the Name property is duplicated, I need to check other objects in the ListCollectionView, if there are objects' Name property equals to this Name, the validate result will be false.

My question is this check can apply only to the edited object, So only this object will become red when its Name is duplicated, but how can I also make check to other list objects, make the other objects which Name property equals to this edited one also become red? Thanks.

1 Answers1

0

You are correct that the ValidationRule only has access to the data it is bound to. However you can write a custom ValidationRule - I've got a blog post detailing it here, and you can then add extra properties to it so you can inject the appropriate data that your bound value should be checked against.

However you will strike one tricky problem - the cells in the datagrid have no natural access to the DataContext of the datagrid because datagrid columns are not in the visual tree. To get around that problem you can use a static proxy object which contains (binds to) the ListCollectionView, this proxy object can then be bound to by the ValidationRule (example 1, example 2).

slugster
  • 49,403
  • 14
  • 95
  • 145
  • Thanks slugster, one question, when I change the TextBox's Text(so the object's Name will also be changed), in TextBox's TextChanged event handler method, can I notify to the object list, and make each object's Name property be validated? Thanks. – user2987339 Jul 05 '17 at 06:02
  • No, it doesn't work like that and if you attempt it you will end up in a right mess real quick :) I have done this before - your best option is to enumerate input controls down through the visual tree, get the binding expression for those input controls, and then trigger any validation rules that might be on the binding expression. This approach should be used sparingly - it's slow. But it is the only real practical way to invalidate other datagrid cells based on the selected one changing. – slugster Jul 05 '17 at 11:49