[英]WPF Style Validation Trigger Command
我有一些帶有驗證器的動態生成的文本框。 如果發生驗證錯誤,我希望他們將命令發送到VM。 此行為以樣式放置,因此無需將其寫入xaml代。
這是代碼:
<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>
我現在遇到的問題是,在VlidationErrorEventTrigger類中調用了“ Onvalidation”事件,但在Viewmodel中未調用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>
這是假設您的觸發器無法找到所需的命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.