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!
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.
精彩评论