WPF 扩展器验证
如果扩展器中的控件中发生 IDataError 验证,有谁知道如何更改扩展器的样式。例如
<Expander Header="Details">
<TextBox Text="{Binding Brand.DESCRIPTION,
UpdateSourceTrigger=LostFocus,
ValidatesOnDataErrors=True}"/>
</Expander>
,如果文本框有错误,我的扩展器的样式将会改变(可能会变成红色)。 我希望使其尽可能通用,以便尽可能不手动绑定到扩展器中的每个控件。
Does anyone know of a way to change the style of an expander if a IDataError validation occurs in a control held within the expander. E.g.
<Expander Header="Details">
<TextBox Text="{Binding Brand.DESCRIPTION,
UpdateSourceTrigger=LostFocus,
ValidatesOnDataErrors=True}"/>
</Expander>
So if the textbox has an error the style of my expander will change (go red maybe).
I'm looking to make this as generic as possible so without binding to each control within the expander manually if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过附加行为使用附加事件
Validation.Error
(每次添加或删除验证错误时都会引发该事件)。要实现此功能,您需要将NotifyOnValidationError=True
添加到绑定中。此附加行为
ChildValidation
订阅Expander
的Validation.Error
事件,如果NotifyOnValidationError
则该事件会冒泡在绑定上设置为 True。由于多个Control
可能位于Expander
内,因此它还需要跟踪当前活动的验证错误计数,以确定是否应显示红色边框。它可能看起来像这样Xaml
ChildValidationBehavior
You could make use of the Attached Event
Validation.Error
(which is raised everytime a validation error is added or removed) through an Attached Behavior. To make this work you need to addNotifyOnValidationError=True
to the bindings.This Attached Behavior,
ChildValidation
, subscribes to theValidation.Error
event for theExpander
which is bubbled up ifNotifyOnValidationError
is set to True on the bindings. Since severalControl
s may be located within theExpander
it also need to keep track of the count of Validation Errors that's currently active to determine if a Red Border should be displayed or not. It could look like thisXaml
ChildValidationBehavior