什么原因可以防止显式和隐式样式的应用? [英] What reasons could prevent explicit and implicit styles from applying?

查看:156
本文介绍了什么原因可以防止显式和隐式样式的应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF测试项目,我用于回答这些问题,不知何故,这个项目随着时间的推移变得相当凌乱,有些东西不能正常工作。这可能不是一个真正的问题,因为我可以把它扔掉,并创建一个新的,但显然这在每一种情况下都不是一个可行的解决方案,所以我认为可能有趣的是知道什么可以导致这种行为。



特别令人惊讶的是,即使显式样式也不适用。例如我有这种风格

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

Window.Resources 我应用到这样一个最低限度的图像:

 <图像宽度=80高度=48样式={StaticResource EnlargeImageStyle}> 
< Image.Source>
< BitmapImage UriSource =pack:// application:,,, / Images / Img.png/>
< /Image.Source>
< / Image>

它只是不会做任何事情,如果我尝试隐式应用我也没有结果



所以如标题所示,什么可以阻止显式和隐式样式的应用?



我不想将此限制在我的问题,任何可能在野外遇到的原因很好。

解决方案

你的代码看起来不错,应该工作。确保没有其他样式或动画不会覆盖你的默认样式。在评估依赖属性的值时,wpf依赖属性系统考虑到一些因素,如动画中设置的值将具有最高优先级。通过以下链接获取更多



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



您可以使用像 Snoop ,以便在运行时间进行调试,以检查实际应用哪些样式。


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)

解决方案

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.

这篇关于什么原因可以防止显式和隐式样式的应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆