I am trying to make a simple mouseover effect on a button, It does change the color when mouse is over but the color is immediately changed to the default button background… how can I override this behavior?
this is my code:
Style myBtnStyle = new Style();
Trigger bla = new Trigger() { Property = IsMouseOverProperty, Value = true };
bla.Setters.Add(new Setter(Control.BackgroundProperty, Brushes.Black));
myBtnStyle.Triggers.Add(bla);
button2.Style = myBtnStyle;
According to this post, that fancy animation is built into and to remove it, you’re going to need to override the
ControlTemplatefor yourButton. Fortunately, that isn’t too hard. I used this post as source material and came up with the followingStylethat gives you the idea.Update:
If you’re dead set on applying the
Stylein code and you don’t want to use aResourceDictionary(probably the better way to do it), you can load theStyledynamically usingXamlReader.Load: