Reputation: 9
I am trying to create a MouseOver
style in the code behind. I have done this succesfully in XAML, but I need to do it in C#. Here is my style code:
Style style = new Style();
style.Setters.Add(new Setter(Button.BackgroundProperty, new BrushConverter().ConvertFrom("#FF0000") as Brush));
Trigger mouseOver = new Trigger() {
Property = IsMouseOverProperty,
Value = true,
};
mouseOver.Setters.Add(new Setter(Button.BackgroundProperty, new BrushConverter().ConvertFrom("#00FF00") as Brush));
style.Triggers.Add(mouseOver);
The button
s apply the style in the XAML code. I can see them change color when this code is run, but the MouseOver
never changes. I've looked at dozens of MSDN articles and Stackoverflow posts about this but none of the solutions worked in my case.
What am I doing wrong here?
Upvotes: 0
Views: 550
Reputation: 627
Suppose the x:Name of the button is equal to "Button1"
You need to add at the end:
<Button x:Name="Button1"/>
Style style = new Style();
style.Setters.Add(new Setter(Button.BackgroundProperty, new
BrushConverter().ConvertFrom("#FF0000") as Brush));
Trigger mouseOver = new Trigger() {
Property = IsMouseOverProperty,
Value = true,
};
mouseOver.Setters.Add(new Setter(Button.BackgroundProperty, new
BrushConverter().ConvertFrom("#00FF00") as Brush));
style.Triggers.Add(mouseOver);
Button1.Style = style;
Upvotes: 1