tags:

views:

37

answers:

2

I am going crazy here! What am I missing and why it is not styling anything:

<Style x:Key="textBoxStyle" TargetType="{x:Type TextBox}">
                <Style.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="Background" Value="Red" />
                    </Trigger>
                </Style.Triggers>
            </Style>

    <TextBox Width="100" Style="{StaticResource textBoxStyle}" Height="20" Background="Yellow" ></TextBox>

The above code does not do anything. It does not highlight the TextBox control!

+2  A: 

This occurs because local values override style values. (Properties set directly on an element have very high precedence.) You are setting Background directly on the TextBox, so WPF is going, "Well, he normally wants textBoxStyle backgrounds to be Red when focused, but for this particular TextBox, he's said he specifically wants Background to be Yellow, so Yellow it is."

So the fix is to move the Yellow background to be part of the Style:

<Style x:Key="textBoxStyle" TargetType="{x:Type TextBox}">
  <Setter Property="Background" Value="Yellow" />
  <Style.Triggers>
    <Trigger Property="IsFocused" Value="True">
      <Setter Property="Background" Value="Red" />
    </Trigger>
  </Style.Triggers>
</Style>

and remove it from the TextBox:

<TextBox Width="100" Style="{StaticResource textBoxStyle}" Height="20" />
itowlson
indeed, i was in a hurry, overriding style values seems to be the actual problem
arconaut
Actually the style is defined in the Window.Resources
azamsharp
That works! but now the question is that what about rich style controls. Should I move everything to styles from the code.
azamsharp
What if I have gradients and very complicated styles?
azamsharp
Isn't it more convenient for you to have all the brushes in resources? So your actual layout would only contain elements declarations with references to styles and some attributes that are different or not included in the style
arconaut
Yes that is true! how can I extract the styles from the controls? Like if my border has gradient and other styles.
azamsharp
Gradients can still be expressed in styles (use XAML property element syntax for Setter.Value), or as arconaut says you can make them separate resources and reference them from the style. I am not sure what you mean by "what about rich style controls" -- styles can be as rich as you need -- but it sounds like that may be worth expanding and raising as a separate question rather than us trying to cover it in comments!
itowlson
Oh, you mean like an "extract properties to style" command? Visual Studio doesn't have that built in but I think Expression Blend does, and I believe XAML Power Toys provides it via a Visual Studio add-in.
itowlson
you got it itowlson! let me check that feature out "extract properties to style".
azamsharp
A: 

Define your Style before the TextBox or use DynamicResource instead of StaticResource

arconaut
Actuall it is defined before the TextBox inside the Window.Resources
azamsharp
Changing from Static to Dynamic did NOT help!
azamsharp