[英]WPF Style Validation Trigger Command
I have some dynamic generated Textboxes with Validators. 我有一些带有验证器的动态生成的文本框。 I want them to send a Command to VM, if a validation error occurs.
如果发生验证错误,我希望他们将命令发送到VM。 This Behavior is placed in a style, so I don't need to write it into the xaml generation.
此行为以样式放置,因此无需将其写入xaml代。
Here's the Code: 这是代码:
<behaviors:Triggers x:Key="validationTrigger" x:Shared="False">
<behaviors:ValidationErrorEventTrigger>
<cmd:EventToCommand Command="{Binding ValidationError,NotifyOnValidationError=True}"
PassEventArgsToCommand="True" />
</behaviors:ValidationErrorEventTrigger>
</behaviors:Triggers>
<Style x:Key="EditableTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="#DDFFDD" />
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="2">
<AdornedElementPlaceholder />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="behaviors:OCCInteraction.Triggers" Value="{StaticResource ResourceKey=validationTrigger}" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="#FFDDDD"/>
</Trigger>
</Style.Triggers>
</Style>
The Problem I'm having now, is that the "Onvalidation" Event is called in the VlidationErrorEventTrigger class, but the Command isn't called in the Viewmodel. 我现在遇到的问题是,在VlidationErrorEventTrigger类中调用了“ Onvalidation”事件,但在Viewmodel中未调用Command。 I've tested it with a direct integration and not with a style and it works this way.
我已经通过直接集成而不是样式对其进行了测试,并且这种方式可以正常工作。
So maybe it has something to do with the Binding of the Command or so... 因此,也许与命令绑定有关……
I hope this description is enough to solve the Problem. 我希望此描述足以解决问题。 If not please tell me :)
如果没有,请告诉我:)
I am not sure how you binding is done but assuming that DataContext
of you `Control/Window has your ViewModel Instance and
ValidationError` is your command... 我不确定您是如何完成绑定的,但是假设您的
`Control/Window has your ViewModel Instance and
DataContext
`Control/Window has your ViewModel Instance and
ValidationError是您的命令...
<behaviors:Triggers x:Key="validationTrigger" x:Shared="False">
<behaviors:ValidationErrorEventTrigger>
<cmd:EventToCommand Command="{Binding Path=DataContext.ValidationError,RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}",NotifyOnValidationError=True}"
PassEventArgsToCommand="True" />
</behaviors:ValidationErrorEventTrigger>
</behaviors:Triggers>
This is assuming that your trigger is not able to find the command required. 这是假设您的触发器无法找到所需的命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.