背景不会更改按钮C#WPF [英] Background does not change of button C# WPF

查看:59
本文介绍了背景不会更改按钮C#WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将鼠标悬停在样式上.

I am trying to change style on mouseover.

我的代码是:

<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 时,它就会起作用.

But the Background is the same default thing. But when i change property to BorderThickness then it works.

我在做什么错了?

推荐答案

在按钮的默认模板中,ControlTemplate中存在触发器,该触发器将按钮的Background设置为#FFBEE6FD ,并且由于控制模板触发器具有 更高的优先级 与样式触发器相比,这就是您的触发器永不起作用的原因.

In the default template of button, there is trigger in ControlTemplate which set Background of button to #FFBEE6FD and since control template triggers have higher precedence compared to Style triggers that's why your trigger never works.

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

For that to achieve you have to override default template of button and remove that trigger from it so that your style trigger gets applied.

这是默认模板,其中注释了该特定触发器.如果您还想覆盖BorderBrush,也可以从模板中删除它.

Here is the default template with that specific trigger commented out. In case you want to override BorderBrush as well, get rid of it as well from the template.

<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进行应用:

Define the template somewhere under resource section of parent panel or UserControl and can be applied via StaticResource:

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

这篇关于背景不会更改按钮C#WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆