Style.Triggers vs ControlTemplate.Triggers

蓝咒 提交于 2019-12-02 20:42:26
ShyKnee

Update from Background does not change of button C# WPF the Button in windows 8 does use a ControlTemplate.Trigger for IsMouseOver so there are cases where ControlTemplate may need to be completely overwritten to get the desired functionality. So that would be a case where you need to use ControlTemplate triggers over Style triggers.

You may not always need to override the default ControlTemplate. Say you have a control and you want all the MyTextControl to have a a blue Foreground when IsMouseOver is true and leave everything else as default. You could use something like this:

<Style TargetType="{x:Type MyTextControl}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="Blue"/>
        </Trigger>
    </Style.Triggers>
</Style>

If you wanted to use the ControlTemplate.Triggers you would need to copy the default MyTextControl Template or else you would end up with no visual.

Aside from that I think the only difference is that Style.Triggers has a lower precedence than ControlTemplate.Triggers (Precedence documantation). But that would only matter if you use both trigger types.

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