简体   繁体   English

消除WPF按钮的鼠标悬停效果

[英]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. 我在WPF中创建了一个Button并为其提供了背景图片。 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. 现在的问题是Windows MouseOver效果可见。 Is there an easy method to remove that? 有一个简单的方法可以删除吗? I tried to replace the Value with an Image . 我试图用Image代替Value It worked but I couldn't set the Text anymore on the button. 它起作用了,但是我不能在按钮上设置Text了。

<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. 我认为最好的方法是重新定义按钮ControlTemplate。 Here the msdn doc . 这是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. 在模板中,您将看到带有很多状态的VisualStateManager类。 One of those states would be MouseOver. 这些状态之一是MouseOver。 Within that MouseOver declaration you can put whatever you want to make the Button look the way you like. 在该MouseOver声明中,您可以放置​​任何想要使Button看起来像您想要的样子的东西。 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. 例如,在VisualStateManager中还应该有一些默认的视觉状态,因此您可以将默认状态下的磁偏角复制到鼠标悬停的视觉状态上,它们将是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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