C# 基于按钮背景色的按钮自动悬停颜色

C# 基于按钮背景色的按钮自动悬停颜色,c#,wpf,button,styles,C#,Wpf,Button,Styles,我在谷歌上搜索了很多次,但在这种情况下我什么也没找到 我有几个平面的按钮具有不同的背景颜色,但它们的悬停颜色都是相同的!我希望每个按钮根据其背景有自己的悬停颜色,例如,颜色稍微浅一点 我尝试过制作自定义样式,并降低了IsMouseOver触发器中的不透明度,但这不是我想要的,因为它还影响按钮的内容,而不仅仅是背景颜色 Win32 C#应用程序有一个平面外观属性,可以自动完成这一任务。当您为背景色选择一种颜色并将按钮设置为平面时,它会处理所有状态的颜色(悬停颜色、禁用颜色等),但是我不知道如何在W

我在谷歌上搜索了很多次,但在这种情况下我什么也没找到

我有几个平面的
按钮
具有不同的
背景
颜色,但它们的悬停颜色都是相同的!我希望每个
按钮
根据其背景有自己的悬停颜色,例如,颜色稍微浅一点

我尝试过制作自定义样式,并降低了
IsMouseOver
触发器中的
不透明度,但这不是我想要的,因为它还影响按钮的内容,而不仅仅是背景颜色

Win32 C#应用程序有一个平面外观属性,可以自动完成这一任务。当您为
背景色选择一种颜色并将按钮设置为平面时,它会处理所有状态的颜色(悬停颜色、禁用颜色等),但是我不知道如何在WPF中执行此操作

提前感谢您

查看以下内容:

对于背景,您可以简单地设置“背景”属性,而不是设置“BitmapEffect”属性

希望这有帮助

关于

请查看以下内容:

the hover color is the same for all of them!

对于背景,您可以简单地设置“背景”属性,而不是设置“BitmapEffect”属性

希望这有帮助

问候

the hover color is the same for all of them!
这可能是因为在
按钮的样式中,当
IsMouseOver
True
时,您有一个背景
笔刷

because it also affects the content of the button and not only the background color
这可能是因为当您正在设置动画的面板(可能是
边框
)也是内容控件的父控件(例如
ContentPresenter
ContentControl
)时,此内容控件的
不透明性也会受到影响

要解决这些问题,您只需在与内容控件相同的级别上创建一些视觉元素(例如,
矩形
边框
),并根据激活的触发器设置它们的动画

下面是一个简单的例子

<Window.Resources>
    <Style x:Key="FocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
    <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
    <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
    <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
    <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FF737B7F"/>
    <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
    <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
    <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
    <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>
    <Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
        <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="4"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid SnapsToDevicePixels="true">
                        <Rectangle x:Name="BackgroundVisual" Fill="{TemplateBinding Background}"/>
                        <Rectangle x:Name="PressedVisual" Fill="{DynamicResource Button.Pressed.Background}" Opacity="0"/>
                        <Rectangle x:Name="DisabledVisual" Fill="{DynamicResource Button.Disabled.Background}" Opacity="0"/>
                        <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsDefaulted" Value="true"/>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Opacity" TargetName="BackgroundVisual" Value="0.6"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Opacity" TargetName="PressedVisual" Value="1"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
                            <Setter Property="Opacity" TargetName="DisabledVisual" Value="1"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderThickness" Value="0"/>
    </Style>
</Window.Resources>

下面是样式的外观

希望这有帮助!:)

这可能是因为在
按钮的样式中,当
IsMouseOver
True
时,您有一个背景
笔刷

because it also affects the content of the button and not only the background color
这可能是因为当您正在设置动画的面板(可能是
边框
)也是内容控件的父控件(例如
ContentPresenter
ContentControl
)时,此内容控件的
不透明性也会受到影响

要解决这些问题,您只需在与内容控件相同的级别上创建一些视觉元素(例如,
矩形
边框
),并根据激活的触发器设置它们的动画

下面是一个简单的例子

<Window.Resources>
    <Style x:Key="FocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
    <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
    <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
    <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
    <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FF737B7F"/>
    <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
    <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
    <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
    <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>
    <Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
        <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="4"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid SnapsToDevicePixels="true">
                        <Rectangle x:Name="BackgroundVisual" Fill="{TemplateBinding Background}"/>
                        <Rectangle x:Name="PressedVisual" Fill="{DynamicResource Button.Pressed.Background}" Opacity="0"/>
                        <Rectangle x:Name="DisabledVisual" Fill="{DynamicResource Button.Disabled.Background}" Opacity="0"/>
                        <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsDefaulted" Value="true"/>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Opacity" TargetName="BackgroundVisual" Value="0.6"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Opacity" TargetName="PressedVisual" Value="1"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
                            <Setter Property="Opacity" TargetName="DisabledVisual" Value="1"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderThickness" Value="0"/>
    </Style>
</Window.Resources>

下面是样式的外观


希望这有帮助!:)

感谢您的回复,但正如我上面所描述的,我应该将什么设置为Background属性以使其更轻?!Mamnun:)如果要使用相同的较浅颜色,请使用HexValue:类似于希望的帮助,那么如果我的背景是绿色,并且我希望鼠标悬停时背景变浅,该怎么办?我要一条一般规则。当您希望悬停事件中的所有按钮都有黄色背景时,您的方法才起作用!正如我前面提到的,我希望当前的背景颜色(不管它的颜色)只是变得更亮一点。。。如果它是绿色,那么得到浅绿色,如果它是蓝色,那么得到浅蓝色…很久以前我做了类似的事情,但不是为了按钮,也许它对你有帮助,我使用不同的x:键的ResourceDictionary,我搜索它,只找到了这个:,注意“Sonhja”提交的检查答案希望这有帮助,否则我认为我不是回答这个问题的合适人选非常感谢你的帮助和关注。我刚查过,不幸的是,这不是我想要的。无论如何,我真的很感谢你们的关心和帮助…谢谢你们的回复,但正如我上面所描述的,我应该设置什么背景属性使它更轻?!Mamnun:)如果要使用相同的较浅颜色,请使用HexValue:类似于希望的帮助,那么如果我的背景是绿色,并且我希望鼠标悬停时背景变浅,该怎么办?我要一条一般规则。当您需要所有按钮时,您的方法才起作用