背景不会改变按钮 C# WPF

Background does not change of button C# WPF

我想在鼠标悬停时更改样式。

我的代码是:

<Button Name="register" Content="Register" Margin="15,410,20,0" Padding="6" FontSize="18" VerticalAlignment="Top" Background="#FF0090D6" BorderBrush="#FF0090D6" Foreground="White">
                    <Button.Style>
                        <Style TargetType="Button">
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="Red"></Setter>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>

但背景是相同的默认设置。但是当我将 属性 更改为 BorderThickness 时,它就起作用了。

我做错了什么?

您必须覆盖默认模板才能实现此目的。

  <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="50" Height="50" HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Margin="50,0,0,0">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Background" Value="Green"/>
                    <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="DarkGoldenrod"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

希望对您有所帮助。

在按钮的默认模板中,ControlTemplate 中有一个触发器,它将按钮的背景设置为 #FFBEE6FD,并且由于控件模板触发器与样式触发器相比具有 higher precedence,这就是您的触发器永远无法工作的原因。

为此,您必须覆盖按钮的默认模板并从中删除该触发器,以便应用您的样式触发器。

这是注释掉特定触发器的默认模板。如果您还想覆盖 BorderBrush,请从模板中也删除它。

<ControlTemplate x:Key="DefaultTemplateOfButton" TargetType="ButtonBase">
    <Border BorderThickness="{TemplateBinding Border.BorderThickness}"
                BorderBrush="{TemplateBinding Border.BorderBrush}"
                Background="{TemplateBinding Panel.Background}"
                Name="border"
                SnapsToDevicePixels="True">
        <ContentPresenter RecognizesAccessKey="True"
                                Content="{TemplateBinding ContentControl.Content}"
                                ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                                ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
                                Name="contentPresenter"
                                Margin="{TemplateBinding Control.Padding}"
                                HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                                SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
                                Focusable="False" />
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="Button.IsDefaulted" Value="True">
            <Setter Property="Border.BorderBrush" TargetName="border">
                <Setter.Value>
                    <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" />
                </Setter.Value>
            </Setter>
        </Trigger>
        <Trigger Property="UIElement.IsMouseOver" Value="True">
            <!--<Setter Property="Panel.Background" TargetName="border">
                <Setter.Value>
                    <SolidColorBrush>#FFBEE6FD</SolidColorBrush>
                </Setter.Value>
            </Setter>-->
            <Setter Property="Border.BorderBrush" TargetName="border">
                <Setter.Value>
                    <SolidColorBrush>#FF3C7FB1</SolidColorBrush>
                </Setter.Value>
            </Setter>
        </Trigger>
        <Trigger Property="ButtonBase.IsPressed" Value="True">
            <Setter Property="Panel.Background" TargetName="border">
                <Setter.Value>
                    <SolidColorBrush>#FFC4E5F6</SolidColorBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="Border.BorderBrush" TargetName="border">
                <Setter.Value>
                    <SolidColorBrush>#FF2C628B</SolidColorBrush>
                </Setter.Value>
            </Setter>
        </Trigger>
        <Trigger Property="ToggleButton.IsChecked" Value="True">
            <Setter Property="Panel.Background" TargetName="border">
                <Setter.Value>
                    <SolidColorBrush>#FFBCDDEE</SolidColorBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="Border.BorderBrush" TargetName="border">
                <Setter.Value>
                    <SolidColorBrush>#FF245A83</SolidColorBrush>
                </Setter.Value>
            </Setter>
        </Trigger>
        <Trigger Property="UIElement.IsEnabled" Value="False">
            <Setter Property="Panel.Background" TargetName="border">
                <Setter.Value>
                    <SolidColorBrush>#FFF4F4F4</SolidColorBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="Border.BorderBrush" TargetName="border">
                <Setter.Value>
                    <SolidColorBrush>#FFADB2B5</SolidColorBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="TextElement.Foreground" TargetName="contentPresenter">
                <Setter.Value>
                    <SolidColorBrush>#FF838383</SolidColorBrush>
                </Setter.Value>
            </Setter>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

How to set the control template to button?

在父面板或 UserControl 的资源部分下的某处定义模板,并且可以通过 StaticResource 应用:

<Grid>
   <Grid.Resources>
      <ControlTemplate x:Key="MyTemplate"
                       TargetType="ButtonBase">
       .......
   </Grid.Resources>
   <Button Name="register" Template="{StaticResource MyTemplate}"...>
</Grid>

编辑: 正如 Rohit 指出的那样

This won't work on Windows 8 because of some changes in PresentationFramework where default template of button is declared. ... In Windows 7 default template of button doesn't have that ControlTemplate trigger. That's why your posted code works fine on Windows7 but it won't work on Windows 8 and higher.


关于 DependencyProperty Precedence 的原因是正确的,但是有一种比覆盖按钮的模板更简单的修复方法。

如果您查看 DependencyProperty Precedence List,您会注意到 <Tag> 中设置的属性的优先级高于 Triggered 属性,这就是为什么您的按钮将始终使用您定义的背景的原因在你的 <Button> 标签中。

如果将 Background 属性 移动到 <Style> 本身,则触发 属性 将起作用,因为触发属性优先于样式中定义的属性。

<Button Name="register" Content="Register" ...>
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="Background" Value="#FF0090D6" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>
 <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="50" Height="50" HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Margin="50,0,0,0">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Background" Value="Green"/>
                    <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="DarkGoldenrod"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

以上代码完美运行, 即使有了这个用户也可以更改 runtime/codebehind

中按钮的背景颜色