我们可以在验证时设置源对象的属性吗?
我有一个 wpf-mvvm 应用程序。
在下面的代码中,“PartBPremiumBuydown”是一个类的实例。它有两个属性 => 1.价值。 2.HasValidationError。
属性“Value”用于绑定到文本框。如果有任何验证错误...我可以设置 HasValidationError=true 吗?
<TextBox ToolTip="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors).CurrentItem.ErrorContent}">
<TextBox.Text>
<Binding Path="PartBPremiumBuydown.Value"
ValidatesOnDataErrors="True"
UpdateSourceTrigger="PropertyChanged"
Converter="{x:Static localns:Converters.DecimalToCurrency}">
<Binding.ValidationRules>
<localns:CurrencyRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
I have a wpf-mvvm application.
In the below code, "PartBPremiumBuydown" is an instance of a class. which has two properties => 1. Value. and 2. HasValidationError.
Property "Value" is used for binding to textbox. If there is any validation error...Can I set HasValidationError=true ?
<TextBox ToolTip="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors).CurrentItem.ErrorContent}">
<TextBox.Text>
<Binding Path="PartBPremiumBuydown.Value"
ValidatesOnDataErrors="True"
UpdateSourceTrigger="PropertyChanged"
Converter="{x:Static localns:Converters.DecimalToCurrency}">
<Binding.ValidationRules>
<localns:CurrencyRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该让
PartBPremiumBuydown
实现IDataErrorInfo
接口,类似于以下代码:现在,当您将 TextBox 绑定到
Value
时,如果用户输入在违反规则的文本中,验证错误将显示在文本框中。You should have
PartBPremiumBuydown
implement theIDataErrorInfo
interface, similar to the below code:Now, when you bind your TextBox to
Value
, if the user enters in text which breaks your rule, the validation error will show on the TextBox.