2

我有下面的 XAML,它试图将窗口中所有文本框的边框设置为红色 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>
4

1 回答 1

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>
于 2010-12-05T20:54:21.380 回答