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

标签 wpf button blend

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

最佳答案

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

<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>

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

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

enter image description here

这是资源字典方式:

enter image description here

创建一个 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>

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

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

然后简单地为您的按钮指定样式:

Style="{StaticResource CustomButtonStyle}"

关于wpf - Blend/WPF 中按钮上的蓝色鼠标悬停效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26850712/

相关文章:

swift - 在 Swift 中重写 CIImage 最终会导致处理器下降到 0% 并卡住

algorithm - 不透明度涂抹工具

android - 在相对布局中格式化按钮大小

android - 我们可以在 Android 中调整按钮的边框吗?

c++ - 为什么我的彩色立方体不适用于 GL_BLEND?

c# - 如何在不单击一次和任何其他第三方工具的情况下在 wpf 中添加软件更新程序?

javascript - 在按钮 id 提交之前需要检查 4 个复选框才能运行 js

c# - 如何使用 ComboBox (WPF) "Fill"GridViewColumn?

wpf - 嵌套 View 模型上的 IOC 容器

c# - 绑定(bind)用户控件的属性 父用户控件的属性 wpf mvvm