I have a global style that sets all my TextBox styles, but in some cases I want to revert just the Foreground color to the original non-custom-style color. I tried using {TemplateBinding Foreground} inside the specific TextBoxes that I wanted to revert. It didn’t end up being valid XAML and I’m not sure that’s the right way anyhow.
Any ideas? Thanks.
There’s a few ways this could be done. If you look at the Precedence List on the MSDN
then you can see that the Forground set in ways 1-8 will override the
Foregroundfrom a default style. The easiest way being just to set the local value in theTextBox.Another thing that you can do is use the
'BasedOn'property of styles to override the other versions. This does require giving a key value to your default style, but that can then be used to also apply the default like in this example:Edit:
In the case that the default style is applying a value and you want to revert it to the base value there are a few ways I can think of, off hand, to get this behavior. You can’t, that I know of, bind back to the default theme value in a generic manner.
We can however do some other things. If we need the style to not apply some properties, we can set the style to
{x:Null}, thus stopping the default style from applying. Or we can give the element it’s own style that does not inherit from the base style and then re-apply only the setters that we need:We could modify the default style so that the Foreground will only be set on certain conditions, such as the Tag being a certain value.