Auto hover color for buttons based on their Background color
NickName:Majid Ask DateTime:2013-11-09T13:27:44

Auto hover color for buttons based on their Background color

I've googled a lot but I've found nothing about this situation.

I have several flat Buttons with different Background colors, but the hover color is the same for all of them! I want each Button has its own hover color based on its background, for example gets a little lighter.

I've tried making custom styles and I reduced the Opacity in IsMouseOver trigger, but It's not what I want because it also affects the content of the button and not only the background color!

Win32 C# applications have a Flat Appearance property that do the trick automatically. When you choose a color for the BackColor and set the button as Flat, it takes care of all states color (Hover color, Disabled color, ...), however I've no idea how to do this in WPF.

Thank you in advance

Copyright Notice:Content Author:「Majid」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19872683/auto-hover-color-for-buttons-based-on-their-background-color

Answers
Justin XL 2013-11-09T12:38:28

the hover color is the same for all of them!\n\n\nThis is probably because in your Button's style, you have one single Background brush when IsMouseOver is True.\n\nbecause it also affects the content of the button and not only the background color\n\n\nThis is probably because when the panel (likely a Border) you are animating is also the parent of the content control(e.g. ContentPresenter, ContentControl), so the Opaciy of this content control is also affected.\n\nTo solve these problems, you just need to create some visual elements (e.g. Rectangles, Borders) at the same level as your content control, and animate them based on which trigger is activated.\n\nHere is a simple example.\n\n<Window.Resources>\n <Style x:Key=\"FocusVisual\">\n <Setter Property=\"Control.Template\">\n <Setter.Value>\n <ControlTemplate>\n <Rectangle Margin=\"2\" SnapsToDevicePixels=\"true\" Stroke=\"{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}\" StrokeThickness=\"1\" StrokeDashArray=\"1 2\"/>\n </ControlTemplate>\n </Setter.Value>\n </Setter>\n </Style>\n <SolidColorBrush x:Key=\"Button.Static.Background\" Color=\"#FFDDDDDD\"/>\n <SolidColorBrush x:Key=\"Button.Static.Border\" Color=\"#FF707070\"/>\n <SolidColorBrush x:Key=\"Button.MouseOver.Background\" Color=\"#FFBEE6FD\"/>\n <SolidColorBrush x:Key=\"Button.MouseOver.Border\" Color=\"#FF3C7FB1\"/>\n <SolidColorBrush x:Key=\"Button.Pressed.Background\" Color=\"#FF737B7F\"/>\n <SolidColorBrush x:Key=\"Button.Pressed.Border\" Color=\"#FF2C628B\"/>\n <SolidColorBrush x:Key=\"Button.Disabled.Background\" Color=\"#FFF4F4F4\"/>\n <SolidColorBrush x:Key=\"Button.Disabled.Border\" Color=\"#FFADB2B5\"/>\n <SolidColorBrush x:Key=\"Button.Disabled.Foreground\" Color=\"#FF838383\"/>\n <Style x:Key=\"MyButtonStyle\" TargetType=\"{x:Type Button}\">\n <Setter Property=\"FocusVisualStyle\" Value=\"{StaticResource FocusVisual}\"/>\n <Setter Property=\"Background\" Value=\"{StaticResource Button.Static.Background}\"/>\n <Setter Property=\"BorderBrush\" Value=\"Transparent\"/>\n <Setter Property=\"Foreground\" Value=\"{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}\"/>\n <Setter Property=\"HorizontalContentAlignment\" Value=\"Center\"/>\n <Setter Property=\"VerticalContentAlignment\" Value=\"Center\"/>\n <Setter Property=\"Padding\" Value=\"4\"/>\n <Setter Property=\"Template\">\n <Setter.Value>\n <ControlTemplate TargetType=\"{x:Type Button}\">\n <Grid SnapsToDevicePixels=\"true\">\n <Rectangle x:Name=\"BackgroundVisual\" Fill=\"{TemplateBinding Background}\"/>\n <Rectangle x:Name=\"PressedVisual\" Fill=\"{DynamicResource Button.Pressed.Background}\" Opacity=\"0\"/>\n <Rectangle x:Name=\"DisabledVisual\" Fill=\"{DynamicResource Button.Disabled.Background}\" Opacity=\"0\"/>\n <ContentPresenter x:Name=\"contentPresenter\" Focusable=\"False\" HorizontalAlignment=\"{TemplateBinding HorizontalContentAlignment}\" Margin=\"{TemplateBinding Padding}\" RecognizesAccessKey=\"True\" SnapsToDevicePixels=\"{TemplateBinding SnapsToDevicePixels}\" VerticalAlignment=\"{TemplateBinding VerticalContentAlignment}\"/>\n </Grid>\n <ControlTemplate.Triggers>\n <Trigger Property=\"IsDefaulted\" Value=\"true\"/>\n <Trigger Property=\"IsMouseOver\" Value=\"true\">\n <Setter Property=\"Opacity\" TargetName=\"BackgroundVisual\" Value=\"0.6\"/>\n </Trigger>\n <Trigger Property=\"IsPressed\" Value=\"true\">\n <Setter Property=\"Opacity\" TargetName=\"PressedVisual\" Value=\"1\"/>\n </Trigger>\n <Trigger Property=\"IsEnabled\" Value=\"false\">\n <Setter Property=\"TextElement.Foreground\" TargetName=\"contentPresenter\" Value=\"{StaticResource Button.Disabled.Foreground}\"/>\n <Setter Property=\"Opacity\" TargetName=\"DisabledVisual\" Value=\"1\"/>\n </Trigger>\n </ControlTemplate.Triggers>\n </ControlTemplate>\n </Setter.Value>\n </Setter>\n <Setter Property=\"BorderThickness\" Value=\"0\"/>\n </Style>\n</Window.Resources>\n\n\nHere is how the style looks.\n\n\n\nHope this helps! :)",


More about “Auto hover color for buttons based on their Background color” related questions

Auto hover color for buttons based on their Background color

I've googled a lot but I've found nothing about this situation. I have several flat Buttons with different Background colors, but the hover color is the same for all of them! I want each Button ha...

Show Detail

MUI Button hover background color and text color

I have created an Appbar component in React.js with 3 buttons in it but I would like to change the color when I hover over those buttons. The background color is #3c52b2 and the text color is #fff. I

Show Detail

change background color and hover colors on refresh

I want to change the website logo color and the hover color of the buttons every time a user refreshes the pages. The colors have to be coordinated. Website Link The logo is actually an image with a

Show Detail

Change background color on hover with jquery

Because I am using variable color for buttons, I need to add it with inline css. The hover color is other variable. I cant add hover selector on inline css so, I must use js. I have try to do it wi...

Show Detail

QTreeView Item Hover/Selected background color based on current color

In my project I have several QTreeView widgets displaying data. The background color of the items in the QTreeView changes depending on the data's type and association with other items. Here is how

Show Detail

Changing a button's mouse hover color BASED ON the background color

I've been trying to figure out on how to change the button's hover color and I found this: But this is only half of what I'm trying to achieve. I want to change the hover color based on the current

Show Detail

Changing Background Color Using JQuery on Hover

I'm currently coding and programming a website using Ruby on Rails but having some issues with a fairly basic element within the page. Basically, my backend system will contain a color that will ch...

Show Detail

Hover change background color but not change the text a color

This is my code, i only can change the "popup1_btn" button color when hover but text color cannot be changes. If using .popup1_btn a:hover, it only change the text's background color and text colo...

Show Detail

Bootstrap Social Buttons Background Color and Hovering Issue

I am using Bootstrap to create the social buttons for Facebook and LinkedIn. When I set the background color to white for the buttons, it doesn't properly cover the button. The white background sta...

Show Detail

VB Change Hover Color on all Buttons

I currently have this code to change the Fore, or back color on all of my buttons: Private Sub SetAllLabelsForeColor(ByVal parent As Control) For Each c As Control In parent.Controls If

Show Detail