Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't know for certain, but I may be able to provide some information that will help.</p> <p>Recently I tried to do something even simpler: change the background color on a button - actually a ToggleButton. I found that setting a ToggleButton's Background property in code did not do what I expected. This surprised me. Search around, and you'll see "how do I Set the background of a ToggleButton?" is a common question, and it seems there is no simple answer. I've learned why it is so complicated. </p> <p>The ToggleButton has a ControlTemplate that provides the visual appearance of the thing in operation. Inside the default ControlTemplate, there are animations provided for various state transitions. When the button gets pressed, there's an animation that WPF performs (I believe this is for WPF4, Silverlight, and not sure about prior versions). When you hover, there's another animation. And so on. <a href="http://msdn.microsoft.com/en-us/library/ms753328%28v=VS.100%29.aspx" rel="nofollow">Button also has a ControlTemplate</a>. There's something called a VisualStateManager that aids in managing all those animations.</p> <p>There are numerous colors used in each animation, all hardcoded into the default (implicit) template, and not exposed by named properties. You cannot change those without opening the hood of the ControlTemplate. The Background property plays only a minor role in the appearance of all these animations. In other words, the Background does not do what you might expect. </p> <p>You may be running into the same thing. I'm not a WPF expert which is why I am hedging.</p> <p>Possible ways to get what you want:</p> <ul> <li><p>instead of setting Background, set the property <code>BorderBrush</code>. This is a brush used to paint the Button, in the default ControlTemplate, I think. </p></li> <li><p>create your own control, simpler, without the animations in the ControlTemplate, and allow Background to behave as you wish.</p></li> <li><p>create a modified ControlTemplate, and apply it as a style to the button in your page. <a href="http://msdn.microsoft.com/en-us/library/cc903963%28v=VS.95%29.aspx" rel="nofollow">Here's an introduction</a>.</p></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload