4

I have a WPF test project which i use for answering SO questions, somehow this project got quite cluttered over time and some stuff does not work correctly anymore.This may not be a real problem since i can just throw it away and create a new one but obviously that is not quite a viable solution in every case so i thought it might be interesting to know what can cause such behavior.

Especially surprising is that even the explicit styles do not apply. e.g. i have this style

<Style x:Key="EnlargeImageStyle" TargetType="{x:Type Image}">
    <Setter Property="LayoutTransform">
        <Setter.Value>
            <ScaleTransform ScaleX="1" ScaleY="{Binding RelativeSource={RelativeSource Self}, Path=ScaleX}"/>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation To="2" Duration="0:0:0.3"
                                     Storyboard.TargetProperty="LayoutTransform.ScaleX"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
            <Trigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation To="1" Duration="0:0:0.3"
                                     Storyboard.TargetProperty="LayoutTransform.ScaleX"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.ExitActions>
        </Trigger>
    </Style.Triggers>
</Style>

Which is define in the Window.Resources and i apply it to a minimally defined Image like this:

<Image Width="80" Height="48" Style="{StaticResource EnlargeImageStyle}">
    <Image.Source>
        <BitmapImage UriSource="pack://application:,,,/Images/Img.png"/>
    </Image.Source>
</Image>

And it just won't do anything, if i try to apply it implicitly i also get no results.

So as the title states, what could prevent explicit and implicit styles from applying?

(I do not want to restrict this to my problem, any reason that one could possibly encounter in the wild is fine)

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
H.B.
  • 166,899
  • 29
  • 327
  • 400

2 Answers2

4

There are two common reasons of why implicit styles are not applied which i want to name:

The targeted element...

  • ...is inside a DataTemplate or ControlTemplate (Application.Resources are exempt from this)
  • ...inherits from the class specified in the TargetType as opposed to being the same.
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Excellent, thank you HB. This explains why MainWindow does not get some property set when a style exists for type Window. – Sam Feb 18 '13 at 21:05
  • @Sam: It's a rather unfortunate limitation :( – H.B. Feb 19 '13 at 01:07
2

Your code looks fine and should work.Make sure that no Other styles or animations are not overriding your default style. While evaluating the value of a dependency property, the wpf dependency property system takes into consideration a few factors like the value set from an animation will have the highest precedence.Go through the below link for more

http://msdn.microsoft.com/en-us/library/ms743230.aspx

You can make use of tools like Snoop for debugging in the run time to check which styles are actually applied.

biju
  • 17,554
  • 10
  • 59
  • 95