简体   繁体   中英

WPF Textbox Trigger IsMouseOver

It's so difficult now with styles in wpf. Why Triggers does not work???

<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>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderThickness" Value="0 0 0 1"></Setter>
                            <Setter Property="BorderBrush" Value="#a70711"></Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>

You need to define a custom 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>

The reason for this is that there are some triggers defined in the default ControlTemplate that cannot be override by simply defining some custom Style triggers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM