当您将鼠标悬停在WPF中的按钮上时,样式触发器是什么?

时间:2010-09-09 05:40:58

标签: wpf xaml

我的问题很简单。我试图在按钮上的MouseOver / Hover时设置WPF中按钮的背景。

<Style x:Key="StandardButton" TargetType="Button">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Background" Value="CornflowerBlue" />
        </Trigger>
    </Style.Triggers>
</Style>

当我按下鼠标按钮时,背景颜色会短暂变为CornFlowerBlue,但随后会更改为默认颜色,这些颜色会显示您在按钮上保留一段时间的时间。

当我留在按钮上时,我需要使用什么触发器属性来保持按钮CornFlowerBlue的背景?

非常感谢

1 个答案:

答案 0 :(得分:1)

这是Button中实现的Button Chrome的默认属性。我已从默认按钮模板中删除了Button chrome并创建了我自己的按钮

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
x:Class="WpfApplication9.Window1"
x:Name="Window"
Title="Window1"
Width="640" Height="480">
<Window.Resources>
    <Style x:Key="ButtonFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1" Margin="2" SnapsToDevicePixels="true"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#F3F3F3" Offset="0"/>
        <GradientStop Color="#EBEBEB" Offset="0.5"/>
        <GradientStop Color="#DDDDDD" Offset="0.5"/>
        <GradientStop Color="#CDCDCD" Offset="1"/>
    </LinearGradientBrush>
    <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
    <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
        <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
        <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}"
                BorderBrush="Black" BorderThickness="2" CornerRadius="5" x:Name="bd">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                        Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                        </Border>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True"/>
                            </MultiTrigger.Conditions>
                            <Setter TargetName="bd" Property="Background" Value="#FFF40000"/>
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#ADADAD"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0.048*"/>
        <ColumnDefinition Width="0.952*"/>
    </Grid.ColumnDefinitions>
    <Button HorizontalAlignment="Left" Margin="106.048,102,0,0" Style="{DynamicResource ButtonStyle1}" VerticalAlignment="Top" Width="75" Content="Button" Grid.Column="1"/>
</Grid>