Wpf按钮背景颜色在聚焦时会发生变化

时间:2016-09-29 10:15:37

标签: c# wpf button

我试图在按钮获得点击/聚焦时更改背景颜色,但没有任何变化。

<Style x:Key="btnInputForm" TargetType="{x:Type Button}">
    <Setter Property="FontSize" Value="22"/>
    <Setter Property="Background" Value="#2f772b"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Width" Value="120"/>
    <Setter Property="Height" Value="23"/>
    <Setter Property="Button.RenderTransformOrigin" Value=".5,.5"/>
    <Setter Property="TextElement.FontSize" Value="14" />
    <Setter Property="TextElement.FontFamily" Value="Calibri" />
    <Setter Property="Control.BorderBrush" Value="#085b09" />
    <Setter Property="Control.BorderThickness" Value="1,1,1,1" />
    <Setter Property="Margin" Value="3,10,3,3" />
    <Setter Property="HorizontalAlignment" Value="Right" />
    <Setter Property="VerticalAlignment" Value="Bottom" />
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property="Background" Value="Yellow"/>
        </Trigger>
    </Style.Triggers>


</Style>

1 个答案:

答案 0 :(得分:0)

  <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="White"/>
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Green"/>
            </Trigger>
            <Trigger Property="IsFocused" Value="True">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Background" Value="Yellow"/>
            </Trigger>
        </Style.Triggers>
    </Style>

按钮:

<Button Content="Click Me" Height="30" Width="100"></Button>