Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply default WPF style

Tags:

styles

wpf

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.

like image 541
xanadont Avatar asked Jun 03 '09 16:06

xanadont


1 Answers

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" />
like image 133
rmoore Avatar answered Nov 06 '22 10:11

rmoore