c# - 在 WPF 中验证多个链接的数据绑定(bind) TextBox 值

标签 c# .net wpf data-binding inotifypropertychanged

我正在尝试寻找一种优雅的方法来验证 WPF 中两个相关的 TextBox 值。

每个文本框的 Text 属性都绑定(bind)到我的类上的一个公共(public)十进制属性,该类为 TwoWay 绑定(bind)实现 INotifyPropertyChanged。

我想验证这两个值:BiggerValue >= SmallerValue >= 0

我已经使用 IDataErrorInfo 和字符串索引器成功地让每个值针对这些条件进行独立验证。

我的问题如下:用户打算减少两个值并从 BiggerValue 开始,因此现在它小于 SmallerValue。对 BiggerValue TextBox 的验证失败(尽管存储了该值)。然后用户移动到 SmallerValue 并将其设置为小于新的 BiggerValue。现在这两个值都再次有效,但是如何让 BiggerValue 文本框自动反射(reflect)其(未更改的)值现在有效?

我应该查看文本框上的 LostFocus() 之类的事件处理程序,还是向属性 setter 添加类似的内容以强制刷新?

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

我的完整代码如下。不知何故,对于这个简单的问题,这一切都感觉相当笨拙和过于复杂。作为 WPF 新手(这是第 2 天),任何关于我的方法的评论,无论多么激进,都将不胜感激。

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));
    }
}

最佳答案

好吧,一个简单的 hackish 方法是也触发为 biggerValue 更改的属性(这将导致刷新对更大值的验证):

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

但是,更可靠的解决方案是创建自定义验证规则并自行设置: http://msdn.microsoft.com/en-us/library/system.windows.data.binding.validationrules.aspx

关于c# - 在 WPF 中验证多个链接的数据绑定(bind) TextBox 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2068923/

相关文章:

c# - 运行时未找到资源 key ?

.net - 为什么在绑定(bind)到它时没有设置我的依赖属性?

c# - 以编程方式触发嵌套控件事件

c# - 如何在 Crystal Reports 中抑制交叉表行标题

c# - 数据库生成的默认值

C#:是否可以使用表达式或函数作为字典中的键?

c# - 在 List 上执行 Expression ToLower 抛出异常

c# - Visual Studio - 如果未检查返回值,是否可以生成警告?

c# - 从 XML 获取值?

c# - 使重复代码通用