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

BfN*_*rin 1 wpf blend button

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

B.K*_*.K. 10

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

<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>
Run Code Online (Sandbox Code Playgroud)

我在这里展示了两个属性更改:Background&Foreground.您可以拥有任意数量的内容,并将其更改为您希望的任何值.如果您不想要任何更改,只需删除Style.Triggers其中的特定属性即可.

这是一个演示,因为你是新手:

在此输入图像描述

这是Resource Dictionary方式:

在此输入图像描述

创建一个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>
Run Code Online (Sandbox Code Playgroud)

将其放在您App.xaml或您合并资源词典的任何位置:

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ButtonStyles.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

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

Style="{StaticResource CustomButtonStyle}"
Run Code Online (Sandbox Code Playgroud)