蓝色鼠标悬停在 Blend / WPF 中的按钮上的效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26850712/
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 12:41:32  来源:igfitidea点击:

Blue mouse over effect on button in Blend / WPF

wpfbuttonblend

提问by BfN_Turin

I just started getting used to WPF using Blend for Visual Studio. I created previous programs with the standart Windows Forms and now want to get to something more modern. But I already encountered a main problem after like 5 Minutes. I added a button with an background image with a transperancy. That worked like a charm, but the problem is that, when I run the application, the button always gets blue when the Mouse hovers it. I do not want this blue effect but can't find the option to disable it in Blend. Hope someone can help me with this stupid question, Windows Forms was a little

我刚刚开始习惯使用 Blend for Visual Studio 的 WPF。我使用标准 Windows 窗体创建了以前的程序,现在想要更现代的东西。但是在 5 分钟后我已经遇到了一个主要问题。我添加了一个带有透明背景图像的按钮。这就像一个魅力,但问题是,当我运行应用程序时,当鼠标悬停在按钮上时,它总是变成蓝色。我不想要这种蓝色效果,但在 Blend 中找不到禁用它的选项。希望有人能帮我解决这个愚蠢的问题,Windows 窗体有点

回答by B.K.

What you're describing is the default state behavior for the button. You would have to create a custom template or style to change it. Example:

您所描述的是按钮的默认状态行为。您必须创建自定义模板或样式来更改它。例子:

<Button Content="Button">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Black"/>
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

I am demonstrating two properties changes here: Background& Foreground. You may have as many as you want and change them to whatever value you wish. If you don't want any changes, simply remove Style.Triggersor a particular property within it.

我在这里演示了两个属性更改:Background& Foreground。您可以拥有任意多的数量,并将它们更改为您想要的任何值。如果您不想进行任何更改,只需删除其中的Style.Triggers某个特定属性即可。

Here's a demo for you, since you're new:

这是给你的演示,因为你是新手:

enter image description here

在此处输入图片说明

Here's the Resource Dictionaryway:

这是Resource Dictionary方法:

enter image description here

在此处输入图片说明

Create a Resource Dictionaryand add a style to it:

创建一个Resource Dictionary并为其添加样式:

<Style x:Key="CustomButtonStyle" TargetType="Button">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Black"/>
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
    </Style.Triggers>
</Style>

Place this in your App.xamlor wherever you merge your resource dictionaries:

将此放在您App.xaml或您合并资源字典的任何地方:

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ButtonStyles.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Then simply specify the style for your button:

然后只需为您的按钮指定样式:

Style="{StaticResource CustomButtonStyle}"