删除按钮上的蓝色突出显示 wpf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31162264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:14:26  来源:igfitidea点击:

remove blue highlight on buttons wpf

c#wpfxaml

提问by Sarthak Agrawal

I am making a WPF project. While running the application, as I take my cursor to a button, it gets highlighted by blue color filled inside it.

我正在制作一个 WPF 项目。在运行应用程序时,当我将光标移到一个按钮上时,它会被其中填充的蓝色突出显示。

Now, I want to remove this highlight... How do I do so?

现在,我想删除此突出显示...我该怎么做?

enter image description here

在此处输入图片说明

回答by CrumblyRock

The link isn't working for me, so I can't see exactly what you are getting at, but I assume that you're trying to change that default blue-ish gradient. You need to override the template for the button in a style, and then you can customize it to look however you want. You can't just set the background color or use triggers to change it without overriding the template, because the default template has some extra stuff in it to show that gradient effect.

该链接对我不起作用,因此我无法确切了解您的意思,但我假设您正在尝试更改默认的蓝色渐变。您需要以样式覆盖按钮的模板,然后您可以自定义它以显示您想要的外观。你不能只设置背景颜色或使用触发器来改变它而不覆盖模板,因为默认模板中有一些额外的东西来显示渐变效果。

Here is an example of a button I have used before that is fairly straightforward. This example uses a trigger to change the foreground color of the button when it is pressed, but will never change the background color.

这是我之前使用过的一个按钮示例,它相当简单。此示例使用触发器在按下按钮时更改按钮的前景色,但永远不会更改背景色。

    <Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="White"/>
    <Setter Property="FontWeight" Value="SemiBold"/>
    <Setter Property="Foreground" Value="{StaticResource BrushBtnText}"/>
    <Setter Property="Margin" Value="8,4,8,4"/>
    <Setter Property="Padding" Value="6"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="BorderBrush" Value="{StaticResource BrushBorderLight}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                 <Border Margin="{TemplateBinding Margin}"
                         Background="{TemplateBinding Background}" 
                         BorderBrush="{TemplateBinding BorderBrush}" 
                         BorderThickness="{TemplateBinding BorderThickness}" 
                         Padding="{TemplateBinding Padding}">
                     <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                 </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="Foreground" Value="{StaticResource BrushBtnText}"/>
        </Trigger>
    </Style.Triggers>
</Style>

Also, if you're interested, here is what the button's default template is:

此外,如果您有兴趣,这里是按钮的默认模板:

<Style 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}">
                    <Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Themes:ButtonChrome>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="true">
                            <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#ADADAD"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

回答by Sarthak Agrawal

Actually, I found the thing where I was going wrong. Whenever my pointer goes to a button, does not change the background of the button or even the foreground of the button, but it changes the background of the border.

实际上,我发现了我出错的地方。每当我的指针指向一个按钮时,不会改变按钮的背景甚至按钮的前景,但它会改变边框的背景。

So, while overwriting the button style, actually the border of the background has to be overwritten and not the button foreground or the background.

因此,在覆盖按钮样式时,实际上必须覆盖背景的边框,而不是按钮前景或背景。

Here is the answer to this question : How to Disable MouseOver Effects

这是这个问题的答案:如何禁用鼠标悬停效果