【问题标题】:How do you disable MouseOver effects on a Button in WPF?如何在 WPF 中禁用按钮上的 MouseOver 效果?
【发布时间】:2010-11-16 12:04:07
【问题描述】:

我正在尝试在 WPF 中禁用按钮上的 MouseOver 效果,或者至少改变它的颜色。

我正在使用以下样式:

<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 中,我认为它会覆盖所有默认行为。但事实并非如此。

有什么建议吗?

【问题讨论】:

    标签: wpf button mouseevent styles


    【解决方案1】:

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

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

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

    <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>
    

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

    【讨论】:

    • 太好了,查理。我现在明白了。干杯。
    • 感谢查理,它帮助很大。
    • 这救了我的命,谢谢。我必须检查用户创建的带有透明按钮的布局,但悬停效果破坏了我的计划。
    • 天才,终于找到了一个适合我的解决方案 - 并且还允许圆形按钮边框。非常感谢:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 2021-05-18
    相关资源
    最近更新 更多