Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

How do you disable MouseOver effects on a Button in WPF?

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

I'm using the following style:

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

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

Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Look what your control template boils down to:

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...