如何设置按钮的悬停和点击前景?

时间:2013-06-13 14:51:10

标签: c# wpf button styles hover

我是Styles的新手,并且不确定创建Button样式的最佳方式,以更改ForegroundIsMouseOver的{​​{1}}。我使用IsPressed作为模板,因为它具有Border以及背景和边框属性。但CornerRadius没有Border属性。使用Foreground作为模板不能正常工作,因为默认外观未被覆盖。

使用Button行为为Button模板设置样式的更好方法是什么?也许在边框内使用标签?

这是App.xaml:

Foreground

和xaml:

<Application.Resources>
    <Style TargetType="Button" x:Key="MyButton">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="MinHeight" Value="34"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="shortcutbutton"  
                    BorderThickness="1"
                    BorderBrush="Black"
                    Background="Gray">
                        <ContentPresenter Margin="2" 
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center" 
                                  RecognizesAccessKey="True"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="shortcutbutton" Property="Background" Value="#FFDADADA" />
                            <Setter TargetName="shortcutbutton" Property="BorderBrush" Value="#EEEEEE" />
                            <!--<Setter TargetName="Border" Property="Foreground" Value="Black" />-->
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="shortcutbutton" Property="Background" Value="DarkGray" />
                            <Setter TargetName="shortcutbutton" Property="BorderBrush" Value="Black" />
                            <!--<Setter TargetName="Border" Property="Foreground" Value="Red" />-->
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

2 个答案:

答案 0 :(得分:3)

您需要将触发器从ControlTemplate移动到Style。像这样:

        <Style TargetType="Button">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="MinHeight" Value="34"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="shortcutbutton"  
                BorderThickness="1"
                BorderBrush="Black"
                Background="Gray">
                        <ContentPresenter Margin="2" 
                              HorizontalAlignment="Center"
                              VerticalAlignment="Center" 
                              RecognizesAccessKey="True"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Foreground" Value="#FFDADADA" />
            </Trigger>
            <Trigger Property="IsPressed" Value="true">
                <Setter Property="Foreground" Value="DarkGray" />
            </Trigger>
        </Style.Triggers>
    </Style>

答案 1 :(得分:2)

您可以做的是在TextElement上设置Border的附加属性,并且Border中的每个文字元素都将具有该颜色:

<ControlTemplate.Triggers>
   <Trigger Property="IsMouseOver" Value="true">
       <Setter TargetName="shortcutbutton" Property="Background" Value="#FFDADADA" />
       <Setter TargetName="shortcutbutton" Property="BorderBrush" Value="#EEEEEE" />
       <Setter TargetName="shortcutbutton" Property="TextElement.Foreground" Value="White"/>
   </Trigger>
   ...
</ControlTemplate.Triggers>