绑定发生在验证之前
我有一个按钮限制在文本块的 Validation.HasError 属性上。文本块具有检查用户输入的值的验证规则。绑定的目的是,如果用户输入错误的数据,则应禁用该按钮。
问题是 ValidationRule 在绑定之后执行。因此,当用户键入错误的值时,绑定会检查文本块的 HasError 属性,该属性为 FALSE,因此按钮处于启用状态。然后执行验证规则。它会导致相反的情况,即 texblock 无效而按钮启用。
我如何指定哪个(绑定和验证)首先执行?
这是 XAML:
<MultiBinding Converter="{StaticResource ButtonVisibilityConverter}" UpdateSourceTrigger="PropertyChanged" Mode="OneWay" >
<Binding ElementName="integerInput" Path="HasValidationError"/>
<Binding ElementName="doubleInput" Path="HasValidationError"/>
</MultiBinding>
</Button.IsEnabled>
我确信事实并非如此。 我所说的文本块控件是一个自定义微调器(带有两个 +/- 按钮的文本框)。微调器的内容绑定到视图模型中的属性。每次我们单击 + 或 - 按钮时,都会执行一个命令并增加或减少文本框中的值。
I have a button bounded on Validation.HasError property of a textblock. The textblock has a validation rule that checks the value entered by user. The purpose of the binding is that the button should be disabled if the user enters a wrong data.
The problem is that the ValidationRule executes AFTER the binding. So when the user types a wrong value, the binding checks the HasError property of the textblock, which is FALSE, so the button is Enabled. And then the validationrule executes. It results in a reversed situation where the texblock is invalid and the button is enabled.
How could I specify which (binding and validation) executes first ?
Here's the XAML:
<MultiBinding Converter="{StaticResource ButtonVisibilityConverter}" UpdateSourceTrigger="PropertyChanged" Mode="OneWay" >
<Binding ElementName="integerInput" Path="HasValidationError"/>
<Binding ElementName="doubleInput" Path="HasValidationError"/>
</MultiBinding>
</Button.IsEnabled>
I'm sure it isn't the case.
The textblock control I'm talking about is a custom spinner (textbox with two +/- buttons). The content of the spinner is bound to a property in the viewmodel. Each time we click on + or - button a command is executed and increments or decrements the value in the textbox.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用什么 XAML 将按钮绑定到文本框?对 HasError 属性的任何更改都应触发绑定以重新评估按钮,无论在哪个阶段执行验证。
您确定您的问题不是当 HasErrors 属性为 false 时您希望 IsEnabled 属性为 true 并且您没有反转绑定中的值吗?
What XAML are you using to bind the button to the textbox? Any change to the HasError property should trigger the binding to re-evaluate on the button regardless of at what stage validation is performed.
Are you sure your problem isn't that when the HasErrors property is false you want the IsEnabled property to be true and you're not reversing the value in the binding?