What reasons could prevent explicit and implicit styles from applying?

半腔热情 提交于 2019-12-23 12:19:18

问题


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)


回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/5831245/what-reasons-could-prevent-explicit-and-implicit-styles-from-applying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!