WPF validation question: how to work with validation, which includes 2 fields

I have 2 text fields on the screen, one of them is "from the sum" of the other "to the sum". The validation rule "out of amount" must be less than "in amount".

Now my question is: when the user enters a pair of amounts in which "from the sum" is greater than the "sum", how to make BOTH of text boxes with a red frame. And when the user corrects the amount (either by decreasing "from the amount" or increasing "to the amount", how to make BOTH of text fields displayed without error?

thanks

My code is as follows:

public partial class Rate : IDataErrorInfo { public Rate() { is_active = true; registered = DateTime.Now; } #region FOR validation public string Error { get { var properties = this.GetType().GetProperties(); foreach (var propertyInfo in properties) { string err = this[propertyInfo.Name]; if (!string.IsNullOrEmpty(err)) { return err; } } return string.Empty; } } public string this[string propertyName] { get { string result = null; if (result == null && "from_amt" == propertyName) { if (from_amt > to_amt) { result = Resources.Validation.Rate_from_amount_value; } } if (result == null && "to_amt" == propertyName) { if (from_amt > to_amt) { result = Resources.Validation.Rate_to_amount_value; } } return result; } } #endregion } 

}

+4
source share
2 answers

When I check a text field that depends on the value of another text field, instead of highlighting, as you mentioned, I intercept the change in value and accept or reject the change.

I found that if I try to change the text box B based on the change in the text box, then AI always runs into problems, but if I accept or reject the change in text box A based on the value in the text box B, it works fine.

Edit:

I assume that you are dealing with text fields for numbers only. If this is the case, I suggest you do what I did. First, for my validation rule, I created a polygon that represents numerical constraints, this allows you to have a linear relationship between the two values. Then, when a single value changes, I check if the current data point intersects with the check polygon. If the data point is outside the polygon, you do not allow this.

The problem you are facing is that if you are on the edge of the polygon, the value seems to be β€œstuck”. To get around this, I created a graphic that shows the test polygon with the current point in it. Thus, if the user presses one value to the limit, they can understand why they can no longer change.

For good measure, I added sliders along each axis of the graph so that they can easily change the value, and I added drag and drop functionality to the point.

Thus, you have a stable check, and it is very clear to the user how to change the values ​​and why they cannot go beyond certain restrictions.

+1
source

See a similar SO question with the accepted answer. Basically, the solution may be to raise a property change notification for both properties from each property set so that they are both re-evaluated when they are changed.

Check management manually in WPF

0
source

All Articles