开发者

How to use the previous value when validating a WPF DataGrid cell entry?

开发者 https://www.devze.com 2022-12-18 20:33 出处:网络
I have a WPF DataGrid.I would like to restrict the values a user may enter in a particular column, such as the following:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消