如何在 WPF 中消除鼠标悬停时按钮的发光

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18122201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:23:34  来源:igfitidea点击:

How to remove Glow of Button on Mouse hover in WPF

c#wpfbuttonstylesmousehover

提问by Sarin Vadakkey Thayyil

I am using a simple button in WPF. I have put an image for the button on background. My problem is, when i move mouse pointer to button it get a default glow and override the image given as background.

我在 WPF 中使用了一个简单的按钮。我在背景上放置了按钮的图像。我的问题是,当我将鼠标指针移动到按钮时,它会获得默认发光并覆盖作为背景给出的图像。

        <Button Grid.Column="3" Name="Play" BorderBrush="Transparent"
            Focusable="False" Width="45" Height="45" Click="Play_Click"
            VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,6,10,6">
            <Button.Background >
                <ImageBrush ImageSource="images/play.png"  />
            </Button.Background>
            <Button.Foreground>
                <ImageBrush ImageSource="images/play.png"  />
            </Button.Foreground>
        </Button>

Please help me.

请帮我。

回答by Kapitán Mlíko

It's defined in Button's ControlTemplate. You should create your own.

它在 Button 的ControlTemplate 中定义。你应该创建你自己的。

for example like this.

例如像这样。

<Style x:Key="MyButtonStyle" TargetType="Button">
  <Setter Property="Template">
     <Setter.Value>
        <ControlTemplate TargetType="Button">
           <Grid Background="{TemplateBinding Background}">
              <ContentPresenter HorizontalAlignment="Center"
                        VerticalAlignment="Center"/>
           </Grid>
        </ControlTemplate>
     </Setter.Value>
  </Setter>
</Style>

Then you can apply this style to your Button. and set Background to that image of yours.

然后您可以将此样式应用于您的按钮。并将背景设置为您的图像。

<Button Style="{StaticResource MyButtonStyle}" Grid.Column="3" Name="Play" BorderBrush="Transparent"
        Focusable="False" Width="45" Height="45" Click="Play_Click"
        VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,6,10,6">
        <Button.Background >
            <ImageBrush ImageSource="images/play.png"  />
        </Button.Background>
</Button>