简体   繁体   中英

Remove mouseover effect of WPF Button

I can't find a simple answer to this problem.

I've created a Button in WPF and gave it a background image. First my problem was the border, but then I was able to remove that with

Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"

Now the problem is that the windows MouseOver effect is visible. Is there an easy method to remove that? I tried to replace the Value with an Image . It worked but I couldn't set the Text anymore on the button.

<Button x:Name="gameBtnAnswer1" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Cursor="Hand" Padding="-4" Margin="0,0,18,0" Height="38" Width="336" HorizontalAlignment="Left" FontSize="16" Foreground="White" Click="gameBtnAnswer1_Click" BorderThickness="0" Focusable="False">
    <Button.Background>
        <ImageBrush ImageSource="themes/blue/button_answer.png" Stretch="None" TileMode="Tile"/>
    </Button.Background>
    <Button.Content>
        Hier steht die Antwort #1
    </Button.Content>
</Button>

Best approach in my opinion is to redefine the button ControlTemplate. Here the msdn doc .

An example template without triggers:

<Style TargetType="Button">
  <Setter Property="SnapsToDevicePixels" Value="true"/>
  <Setter Property="OverridesDefaultStyle" Value="true"/>
  <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
  <Setter Property="MinHeight" Value="23"/>
  <Setter Property="MinWidth" Value="75"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Border 
          x:Name="Border"  
          CornerRadius="2" 
          BorderThickness="1"
          Background="{StaticResource NormalBrush}"
          BorderBrush="{StaticResource NormalBorderBrush}">
          <ContentPresenter 
            Margin="2"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            RecognizesAccessKey="True"/>
        </Border>
        <ControlTemplate.Triggers>




        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

What comes to mind first is to redesign template for the button. In the template you will see VisualStateManager class with a lot of states. One of those states would be MouseOver. Within that MouseOver declaration you can put whatever you want to make the Button look the way you like. For example, there also should be some default visual state in that VisualStateManager so you can just copy declration from default visual state to mouseover visual state and they will be the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM