MultiTrigger不工作当WPF中的Validation.HasError和IsMouseOver时

时间:2014-08-30 14:00:37

标签: c# .net wpf validation xaml

我有TextBox

<TextBox IsEnabled="{Binding IsChecked, ElementName=Cb_AllowDeletingPictures}"
                 Style="{DynamicResource TextBoxInError}">
     <TextBox.Text>
          <Binding Path="TimeBeforeDeletingPicture" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
               <Binding.ValidationRules>
                    <helpers:TimeBeforeDeletingRule/>
               </Binding.ValidationRules>
          </Binding>
     </TextBox.Text>
</TextBox>

TextBoxInError样式包含Validation.ErrorTemplate属性的模板和设置工具提示的MultiTrigger,并将BorderBrushForeground变为红色,这是它的样子:

<Style x:Key="TextBoxInError" TargetType="{x:Type TextBox}">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel>
                        <TextBlock Foreground="Red" FontSize="20" FontWeight="Bold" Text="!" FontFamily="Segoe UI Light"></TextBlock>
                        <AdornedElementPlaceholder/>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="Validation.HasError" Value="True"></Condition>
                    <Condition Property="IsMouseOver" Value="True"></Condition>
                </MultiTrigger.Conditions>
                <Setter Property="Foreground" Value="Red" />
                <Setter Property="BorderBrush" Value="Red"/>
                <Setter Property="ToolTip"
                    Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/>
            </MultiTrigger>
        </Style.Triggers>
</Style>

现在,正如您在下一个屏幕截图中看到的那样,ErrorTemplate在TextBox之前显示一个红色感叹号就可以了,问题是:

  • 当鼠标is not over为TextBox时,Foreground和BorderBrush都不是红色,这是正常的。
  • 现在,当鼠标is over为TextBox区域时,只有Foreground变为红色,而BorderBrush在鼠标悬停时保持其原始行为。

当鼠标未在TextBox区域上时:

When not MouseOver

当鼠标位于TextBox区域时:

When isMouseOver

为什么会有这样的行为,为什么它在前景中工作而不是在BorderBrush上?我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

TextBox的默认模板中,定义了触发器,在MouseOver上将BorderBrush设置为您在鼠标悬停上看到的蓝色。这就是你的触发器无法工作的原因。您必须 覆盖默认模板并删除该触发器才能使其正常工作

以类似的方式声明模板(我已从控件模板中删除了默认触发器)

<Style x:Key="TextBoxInError" TargetType="TextBox">
    <!-- Your other setters -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBoxBase">
                <Border
                    BorderThickness="{TemplateBinding Border.BorderThickness}"
                    BorderBrush="{TemplateBinding Border.BorderBrush}"
                    Background="{TemplateBinding Panel.Background}"
                    Name="border"
                    SnapsToDevicePixels="True">
                    <ScrollViewer
                        HorizontalScrollBarVisibility="Hidden"
                        VerticalScrollBarVisibility="Hidden"
                        Name="PART_ContentHost"
                        Focusable="False" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="UIElement.IsEnabled"
                             Value="False">
                        <Setter Property="UIElement.Opacity"
                                TargetName="border" 
                                Value="0.56"/>
                    </Trigger>
                    <Trigger Property="UIElement.IsKeyboardFocused"
                             Value="True">
                        <Setter Property="Border.BorderBrush"
                                TargetName="border">
                            <Setter.Value>
                                <SolidColorBrush>#FF569DE5</SolidColorBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <!-- Your triggers -->
</Style>

我从默认模板中删除的触发器如下所示:

<Trigger Property="UIElement.IsMouseOver" Value="True">
    <Setter
        Property="Border.BorderBrush"
        TargetName="border">
        <Setter.Value>
            <SolidColorBrush>#FF7EB4EA</SolidColorBrush>
        </Setter.Value>
    </Setter>
</Trigger>