WPF文本框触发器IsMouseOver

时间:2018-02-04 17:34:32

标签: wpf

现在使用wpf中的样式非常困难。为什么触发器不起作用???

ng g s myservice

1 个答案:

答案 0 :(得分:0)

您需要定义自定义ControlTemplate

<TextBox x:Name="txbUsername" VerticalAlignment="Center" HorizontalAlignment="Center"
         Width="350" Height="20" Margin="5 0 0 0" BorderThickness="0 0 0 1" Background="#eff0f1">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="FontSize" Value="14"></Setter>
            <Setter Property="FontFamily" Value="Verdana, Geneva, sans-serif"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="true">
                                <Setter Property="BorderThickness" Value="0 0 0 1"></Setter>
                                <Setter Property="BorderBrush" Value="#a70711"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TextBox.Style>
</TextBox>

原因是默认ControlTemplate中定义的某些触发器无法通过简单地定义一些自定义Style触发器来覆盖。