views:

301

answers:

2

Hello there,

I have 2 textbox on the screen, one is 'from amount' the other 'to amount'. The validation rule is 'from amount' should be less than 'to amount'.

Now my question is ,when user enters a pair of amounts in which 'from amount' is grater than 'to amount', how to make BOTH of the text boxes display with red border. And when user corrects the amounts (either by decreasing 'from amount' or increasing 'to amount' , how to make BOTH of the text boxes display without error appearance ?

Thanks

My code looks like this :

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


}

}

A: 

When I validate a textbox that depends on the value of another textbox, instead of highlighting like you've mentioned I intercept the value change and accept or reject the change.

I have found that if I try to change textbox B based on a change to textbox A I always run into problems, but if I accept or reject a change to textbox A based on the value in textbox B it works fine.

Edit:

I'm assuming you're dealing with numeric-only textboxes. If that's the case I suggest you do what I did. First, for my validation rule, I created a polygon that represents the numeric constraints, this lets you have linear relationships between the two values. Then, whenever one value is changed I check to see if the current data point intersects with the validation polygon. If the data point rests outside the polygon then you don't allow the change to be made.

The problem you run into is that if you are at the edge of the polygon, then the value seems to be "stuck". To get around this I created a graphic that shows the validation polygon with the current point in it. That way if the user pushes one value to the limit, they can see why they can't change any more.

For good measure I've added slider bars along each axis of the plot so they can change the value easy and I've added drag functionality to the point.

This way, you have stable validation, and it is very clear to the user how to change the values and why they can't go beyond certain limits.

Eric
Thanks Eric for your answer. My customer would like a litter more tolerant option so that she can adjust both from_amt and to_amt. It would be nice to continue using the Validation schema WPF provides.
Sebastian Luo
Added more to the comment above.
Eric
A: 

See a similar SO question with its accepted answer. Basically a solution might be to raise a property change notification for both properties from each property's setter, so that they are both revalidated when either is changed.

Validate control manually in WPF

Aviad P.
I tried to raise property change notification in OnPropertyChange handler as such : when the changed field is "from_amt",raise "to_amt" , this kinda works.However when I added one more piece of code to raise "from_amt" when "to_amt" is being changed, I got stack overflow exception.
Sebastian Luo
Show the code for the `set` accessor for `from_amt` and `to_amt` and show the details of the exception.
Aviad P.