tags:

views:

679

answers:

1

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.

+2  A: 

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 Foreground from a default style. The easiest way being just to set the local value in the TextBox.

<TextBox Foreground="Red" />

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:

    <Style TargetType="{x:Type TextBox}"
     x:Key="myTextBoxStyle">
  <Setter Property="Foreground"
    Value="Red" />
  <Setter Property="FontWeight"
    Value="Bold" />
 </Style>
 <!-- Style applies to all TextBoxes -->
 <Style TargetType="{x:Type TextBox}"
     BasedOn="{StaticResource myTextBoxStyle}" />


<TextBox Text="Hello">
 <TextBox.Style>
  <Style BasedOn="{StaticResource myTextBoxStyle}" TargetType="{x:Type TextBox}">
   <Setter Property="Foreground"
     Value="Blue" />
  </Style>
 </TextBox.Style>
</TextBox>


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:

        <TextBox Text="Hello" Style="{x:Null}" />
  <TextBox Text="Hello">
   <TextBox.Style>
    <Style TargetType="{x:Type TextBox}">
     <Setter Property="FontWeight"
       Value="Bold" />
    </Style>
   </TextBox.Style>
  </TextBox>

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.

    <Style TargetType="{x:Type TextBox}"
     x:Key="myTextBoxStyle">
  <Setter Property="FontWeight"
    Value="Bold" />
  <Style.Triggers>
   <Trigger Property="Tag"
      Value="ApplyForeground">
    <Setter Property="Foreground"
      Value="Red" />
   </Trigger>
  </Style.Triggers>
 </Style>

   <TextBox Text="Hello" />
    <TextBox Text="Hello" Tag="ApplyForeground" />
rmoore
In your example, I don't want to have to specify "Blue". I want to do something like <Setter Property="Foreground" Value="{TemplateBinding Foreground}" />. I want its default w/o explicitly saying Value="Black".
xanadont
Ah, that's a little different indeed. I'll edit my answer to handle that case.
rmoore
Turns out my issue was a little different from what I described. It was really a matter of styling being applied to TextBoxes in a ListView. But I'll accept this answer to give you some SO juice since you did such a great job explaining. Thanks!
xanadont
Thanks, glad it's solved. Also, I found a better way to assign the BasedOn property of a style. If you use BasedOn="{StaticResource {x:Type TextBox}}" then the style will automatically be based on whatever the default applied style for that type is. I think that's a lot better then having to create two styles like in my first code example.
rmoore