How to remove default mouse-over effect on WPF buttons?

后端 未结 8 481
北荒
北荒 2020-11-28 05:42

My problem is that in WPF, whenever I try and change the colour of a button\'s background using triggers or animations, the default mouseover effect (of being grey with that

相关标签:
8条回答
  • 2020-11-28 06:03

    Using a template trigger:

    <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="White"></Setter>
        ...
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="White"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    0 讨论(0)
  • 2020-11-28 06:04

    An extension on dodgy_coder's answer which adds support for..

    • Maintaining WPF button style
    • Adds support for IsSelected and hover, i.e. a toggled button

          <Style x:Key="Button.Hoverless" TargetType="{x:Type ButtonBase}">
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="{x:Type ButtonBase}">
                          <Border Name="border"
                                  BorderThickness="{TemplateBinding BorderThickness}"
                                  Padding="{TemplateBinding Padding}"
                                  BorderBrush="{TemplateBinding BorderBrush}"
                                  Background="{TemplateBinding Background}">
                              <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                          </Border>
                          <ControlTemplate.Triggers>
                              <MultiTrigger>
                                  <MultiTrigger.Conditions>
                                      <Condition Property="IsMouseOver" Value="True" />
                                      <Condition Property="Selector.IsSelected" Value="False" />
                                  </MultiTrigger.Conditions>
                                  <Setter Property="Background" Value="#FFBEE6FD" />
                              </MultiTrigger>
      
                              <MultiTrigger>
                                  <MultiTrigger.Conditions>
                                      <Condition Property="IsMouseOver" Value="True" />
                                      <Condition Property="Selector.IsSelected" Value="True" />
                                  </MultiTrigger.Conditions>
                                  <Setter Property="Background" Value="#BB90EE90" />
                              </MultiTrigger>
      
                              <MultiTrigger>
                                  <MultiTrigger.Conditions>
                                      <Condition Property="IsMouseOver" Value="False" />
                                      <Condition Property="Selector.IsSelected" Value="True" />
                                  </MultiTrigger.Conditions>
                                  <Setter Property="Background" Value="LightGreen" />
                              </MultiTrigger>
      
                              <Trigger Property="IsPressed" Value="True">
                                  <Setter TargetName="border" Property="Opacity" Value="0.95" />
                              </Trigger>
                          </ControlTemplate.Triggers>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      

    examples..

    <Button Content="Wipe On" Selector.IsSelected="True" /> <Button Content="Wipe Off" Selector.IsSelected="False" />

    0 讨论(0)
  • 2020-11-28 06:05

    If someone doesn't want to override default Control Template then here is the solution.

    You can create DataTemplate for button which can have TextBlock and then you can write Property trigger on IsMouseOver property to disable mouse over effect. Height of TextBlock and Button should be same.

    <Button Background="Black" Margin="0" Padding="0" BorderThickness="0" Cursor="Hand" Height="20">
        <Button.ContentTemplate>
            <DataTemplate>
                <TextBlock Text="GO" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" TextDecorations="Underline" Margin="0" Padding="0" Height="20">
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Style.Triggers>
                                <Trigger Property ="IsMouseOver" Value="True">
                                    <Setter Property= "Background" Value="Black"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </DataTemplate>
        </Button.ContentTemplate>
    </Button>
    
    0 讨论(0)
  • 2020-11-28 06:07

    This Link helped me alot http://www.codescratcher.com/wpf/remove-default-mouse-over-effect-on-wpf-buttons/

    Define a style in UserControl.Resources or Window.Resources

     <Window.Resources>
            <Style x:Key="MyButton" TargetType="Button">
                <Setter Property="OverridesDefaultStyle" Value="True" />
                <Setter Property="Cursor" Value="Hand" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Name="border" BorderThickness="0" BorderBrush="Black" Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Opacity" Value="0.8" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Window.Resources>
    

    Then add the style to your button this way Style="{StaticResource MyButton}"

    <Button Name="btnSecond" Width="350" Height="120" Margin="15" Style="{StaticResource MyButton}">
        <Button.Background>
            <ImageBrush ImageSource="/Remove_Default_Button_Effect;component/Images/WithStyle.jpg"></ImageBrush>
        </Button.Background>
    </Button>
    
    0 讨论(0)
  • 2020-11-28 06:20

    The Muffin Man had a very simple answer which worked for me.

    To add a little more specific direction, at least for VS 2013:

    • Right-click the control
    • Select Edit Template => Edit a copy...
    • I selected 'Application' for where to save the style
      • From here you can directly edit App.xaml and see the intuitively named properties. For my purposes, I just set RenderMouseOver="False"
    • Then, in the MainWindow.xaml or wherever your GUI is, you can paste the new style at the end of the Button tag, e.g. ... Style="{DynamicResource MouseOverNonDefault}"/>
    0 讨论(0)
  • 2020-11-28 06:21

    You need to create your own custom button template to have full control over the appearance in all states. Here's a tutorial.

    0 讨论(0)
提交回复
热议问题