views:

31

answers:

1

I have a WPF DataGrid. I would like to restrict the values a user may enter in a particular column, such as the following:

Column A values may only increase (new value > old value).

Column B values may only decrease (new value < old value).

If the user-entered value is invalid, I want to revert to the previous valid value.

I thought that I might be able to do this in a custom ValidationRule, but I don't see a way to access the previous value of the cell from the Validate() function.

I would appreciate any help!

A: 

You need to do this logic in the business layer. Make sure you dont have UpdateSourceTrigger set to be PropertyChanged as this will update your values on every keystroke. if the value was 9 and the user types 10 the update will happen when the user types 1 which will be less than 9 which will reset the value to 9.

A better solution is to flag the field as invalid (easy if you are binding to an object) and show the user the entry is invalid, rather than just changing what they have just entered. Sometimes its only out by one letter and they might just want to change that to make the input valid.

Aran Mulholland
Thanks for the advice, Aran. Can you direct me to any documentation that says this type of logic should be handled in the domain object underlying the UI instead of by the UI itself? My client is determined that the validation of the user's input should happen at the UI layer. Thank you!
DTayntor