如何在 WPF 中的按钮上禁用鼠标悬停效果? [英] How do you disable MouseOver effects on a Button in WPF?

查看:32
本文介绍了如何在 WPF 中的按钮上禁用鼠标悬停效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 WPF 中禁用按钮上的鼠标悬停效果,或者至少更改它的颜色.

I'm trying to disable the MouseOver effect on buttons, or at least change the colour of it, in WPF.

我正在使用以下样式:

<Style x:Key="Borderless" TargetType="{x:Type Button}">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Button Background="{TemplateBinding Control.Background}"
                                Focusable="False">
                            <ContentPresenter
                  Margin="{TemplateBinding Control.Padding}"
                  HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                  VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                  SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
                  ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                  RecognizesAccessKey="True"
                  Content="{TemplateBinding ContentControl.Content}" />
                            </Button>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

在 Window.Resources 中,我认为它会覆盖所有默认行为.但它没有.

in Window.Resources, which I thought would override all the default behaviours. But it doesn't.

有什么建议吗?

推荐答案

看看你的控件模板归结为:

Look what your control template boils down to:

<ControlTemplate TargetType="{x:Type Button}">
   <Button>
      <ContentPresenter/>
   </Button>
</ControlTemplate>

您是说,我想用...一个按钮替换我的按钮的外观."ControlTemplate 的用途是替换控件的可视化树.因此,您正在用另一个按钮替换现有按钮的可视化树.如果您想从头开始创建按钮,请尝试使用 SimpleStyles 按钮:

You're saying, "I want to replace the look of my button with... a button." The usage of the ControlTemplate is to replace the visual tree of a control. So you are replacing the visual tree of the existing button with another button. If you want to start a button from scratch, try using the SimpleStyles button:

<Style TargetType="{x:Type Button}">
   <Setter Property="SnapsToDevicePixels" Value="true"/>
   <Setter Property="OverridesDefaultStyle" Value="true"/>
   <Setter Property="MinHeight" Value="23"/>
   <Setter Property="MinWidth" Value="75"/>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type Button}">
            <Border Name="Border" CornerRadius="2" BorderThickness="1"
                    Background="#C0C0C0"
                    BorderBrush="#404040">
               <ContentPresenter Margin="2" 
                                 HorizontalAlignment="Center"
                                 VerticalAlignment="Center" 
                                 RecognizesAccessKey="True"/>
            </Border>
            <ControlTemplate.Triggers>
               <Trigger Property="IsKeyboardFocused" Value="true">
                  <Setter TargetName="Border" 
                          Property="BorderBrush" Value="#202020" />
               </Trigger>
               <Trigger Property="IsDefaulted" Value="true">
                  <Setter TargetName="Border" 
                          Property="BorderBrush" Value="#202020" />
               </Trigger>
               <Trigger Property="IsMouseOver" Value="true">
                  <Setter TargetName="Border" 
                          Property="Background" Value="#808080" />
               </Trigger>
               <Trigger Property="IsPressed" Value="true">
                  <Setter TargetName="Border" 
                          Property="Background" Value="#E0E0E0" />
                  <Setter TargetName="Border" 
                          Property="BorderBrush" Value="#606060" />
               </Trigger>
               <Trigger Property="IsEnabled" Value="false">
                  <Setter TargetName="Border" 
                          Property="Background" Value="#EEEEEE" />
                  <Setter TargetName="Border" 
                          Property="BorderBrush" Value="#AAAAAA" />
                  <Setter Property="Foreground" Value="#888888"/>
               </Trigger>
            </ControlTemplate.Triggers>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

请注意,此模板以最简单的方式创建按钮:包含按钮内容的边框.它不使用嵌入在模板中的另一个按钮.

Notice that this template creates a button the simplest possible way: a border that contains the button content. It does not use another button embedded inside the template.

这篇关于如何在 WPF 中的按钮上禁用鼠标悬停效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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