一旦在IsMouseOver Trigger上设置,TextBox上的WPF BorderBrush就会消失

时间:2010-12-05 20:01:49

标签: wpf xaml triggers styles brush

我有下面的XAML,它试图将窗口中所有TextBox的边框设置为红色OnMouseOver。当鼠标位于文本框上时,会设置FontSize和Foreground属性,但BorderBrush仅在恢复到其先前的默认值之前暂时设置。我希望BorderBrush保持红色,直到鼠标不再在文本框上。任何想法为什么会这样?

<Window x:Class="StylesApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Width" Value="250" />
            <Setter Property="Height" Value="50" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="FontSize" Value="20" />
                    <Setter Property="Foreground" Value="Red" />
                    <Setter Property="BorderBrush" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <TextBox>
           My TextBox
        </TextBox>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

当IsMouseOver属性设置为true时,我假设TextBox有另一个BorderBrush动画。但是,此触发器仅在BorderThickness正好为1.0时才有效。因此,要克服这个问题,您可以将BorderThickness更改为1.01或触发器中的某些内容,只要鼠标位于TextBox上,BorderBrush就会保持红色。

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Width" Value="250" />
    <Setter Property="Height" Value="50" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderThickness" Value="1.01" />
            <Setter Property="FontSize" Value="20" />
            <Setter Property="Foreground" Value="Red" />
            <Setter Property="BorderBrush" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>
相关问题