如何创建WPF验证规则以确保两个文本字段具有相同的值? [英] How to create a WPF validation rule to ensure two text fields have the same value?
本文介绍了如何创建WPF验证规则以确保两个文本字段具有相同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建验证规则以确保Value2与Value1具有相同的值?如果验证规则不是最佳方法,那么哪个更好呢?我可以让TextChanged事件处理此事件,但我想知道是否还有更优雅的方法.
< TextBox名称="Value1TextBox">< TextBox.Text>< BindingPath Path ="Value1" UpdateSourceTrigger ="PropertyChanged"/></TextBox.Text></TextBox>< TextBox Name ="Value2TextBox">< TextBox.Text>< BindingPath Path ="Value2" UpdateSourceTrigger ="PropertyChanged"/></TextBox.Text></TextBox>
解决方案
就像实现 System.ComponentModel.IDataErrorInfo
接口并添加 ValidatesOnDataErrors
一样.
>
< TextBox名称="Value2TextBox">< TextBox.Text>< BindingPath Path ="Value2" UpdateSourceTrigger ="PropertyChanged" ValidatesOnDataErrors ="True"/></TextBox.Text></TextBox>
How would I create a validation rule to ensure Value2 has the same value as Value1? If a validation rule is not the best method then what would be better? I could have the TextChanged event handle this, but I'm wondering if there is something more elegant.
<TextBox Name="Value1TextBox">
<TextBox.Text>
<BindingPath Path="Value1" UpdateSourceTrigger="PropertyChanged" />
</TextBox.Text>
</TextBox>
<TextBox Name="Value2TextBox">
<TextBox.Text>
<BindingPath Path="Value2" UpdateSourceTrigger="PropertyChanged" />
</TextBox.Text>
</TextBox>
解决方案
Looks like implementing the System.ComponentModel.IDataErrorInfo
interface and adding ValidatesOnDataErrors
did the trick.
<TextBox Name="Value2TextBox">
<TextBox.Text>
<BindingPath Path="Value2" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True" />
</TextBox.Text>
</TextBox>
这篇关于如何创建WPF验证规则以确保两个文本字段具有相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文