开发者

Validating several linked databound TextBox values in WPF

开发者 https://www.devze.com 2022-12-16 23:24 出处:网络
I am trying to find an elegant way to validate two related TextBox values in WPF. The Text property of each text box is bound to a public decimal property on my class which implements INotifyProperty

I am trying to find an elegant way to validate two related TextBox values in WPF.

The Text property of each text box is bound to a public decimal property on my class which implements INotifyPropertyChanged for TwoWay binding.

I want to validate the two values such that: BiggerValue >= SmallerValue >= 0

I have succeeded in getting each value to validate independently against these conditions using IDataErrorInfo and a string indexer.

My problem is as follows: the user intends to reduce both values and starts with BiggerValue, so that now it is less than SmallerValue. The validation on the BiggerValue TextBox fails (although the value is stored). The user then moves on to the SmallerValue and sets it less than the new BiggerValue. Now both values are valid again, but how can I get the BiggerValue text box to automatically reflect that its (unchanged) value is now valid?

Should I be looking at an event handler like LostFocus() on the text boxes, or adding something like this to the property setters to force a refresh?

biggerValueTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
smallerValueTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();

My full code is below. Somehow it all feels rather clunky and overcomplicated for this simple problem. As a WPF newbie (this is day 2), any comments about my approach, however radical, would be gratefully received.

XAML:

<TextBox x:Name="biggerValueTextBox"
         Text="{Binding Path=BiggerValue,
                        Mode=TwoWay,
 开发者_运维知识库                       ValidatesOnDataErrors=True,
                        ValidatesOnExceptions=True}" />
<TextBox x:Name="smallerValueTextBox"
         Text="{Binding Path=SmallerValue,
                        Mode=TwoWay,
                        ValidatesOnDataErrors=True,
                        ValidatesOnExceptions=True}" />

C#:

public partial class MyClass : UserControl,
    INotifyPropertyChanged, IDataErrorInfo
{
    // properties
    private decimal biggerValue = 100;
    public decimal BiggerValue
    {
        get
        {
            return biggerValue;
        }
        set
        {
            biggerValue = value;
            OnPropertyChanged("BiggerValue");
        }
    }
    private decimal smallerValue = 80;
    public decimal SmallerValue
    {
        get
        {
            return smallerValue;
        }
        set
        {
            smallerValue = value;
            OnPropertyChanged("SmallerValue");
        }
    }

    // error handling
    public string this[string propertyName]
    {
        get
        {
            if (propertyName == "BiggerValue")
            {
                if (BiggerValue < SmallerValue)
                    return "BiggerValue is less than SmallerValue.";
                if (BiggerValue < 0)
                    return "BiggerValue is less than zero.";
            }
            if (propertyName == "SmallerValue")
            {
                if (BiggerValue < SmallerValue)
                    return "BiggerValue is less than SmallerValue.";
                if (SmallerValue < 0)
                    return "SmallerValue is less than zero.";
            }
            return null;
        }
    }
    // WPF doesn't use this property.
    public string Error { get { return null; } }

    // event handler for data binding
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}


well, an easy hackish method would be to fire property changed for the biggerValue as well (this will cause a refresh of the validation for the bigger value):

 public decimal SmallerValue
    {
        get
        {
            return smallerValue;
        }
        set
        {
            bool fireForBigger = smallerValue > biggerValue && smallerValue < value;
            smallerValue = value;
            OnPropertyChanged("SmallerValue");
            if (fireForBigger)
            {
                OnPropertyChanged("BiggerValue");
            }
        }
    }

But, a more solid solution would be to create custom validation rules and set it all up on your own: http://msdn.microsoft.com/en-us/library/system.windows.data.binding.validationrules.aspx

0

精彩评论

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