开发者

What reasons could prevent explicit and implicit styles from applying?

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开发者_如何学Go="{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)


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜