wpf Style.Triggers 与 ControlTemplate.Triggers
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26769314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Style.Triggers vs ControlTemplate.Triggers
提问by DeMama
When should I choose Style.Triggersand when should I choose ControlTemplate.Triggers? Are there any benefits using one over another?
什么时候该选择Style.Triggers,什么时候该选择ControlTemplate.Triggers?使用一个比另一个有什么好处吗?
Say I have these styles that achieve the same result:
假设我有这些实现相同结果的样式:
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Triggers>
...
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
...
</Setter>
<Style.Triggers>
...
</Style.Triggers>
</Style>
采纳答案by ShyKnee
Updatefrom Background does not change of button C# WPFthe Button in windows 8 does use a ControlTemplate.Trigger for IsMouseOver so there are cases where ControlTemplatemay 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.
从后台更新不会更改按钮 C# WPFWindows 8 中的按钮确实使用 ControlTemplate.Trigger for IsMouseOver 因此在某些情况下ControlTemplate可能需要完全覆盖以获得所需的功能。因此,在这种情况下,您需要使用 ControlTemplate 触发器而不是 Style 触发器。
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 Foregroundwhen IsMouseOveris true and leave everything else as default. You could use something like this:
您可能并不总是需要覆盖默认的ControlTemplate. 假设您有一个控件,并且您希望所有 MyTextControlForeground在IsMouseOver为 true时都具有蓝色,并将其他所有内容保留为默认值。你可以使用这样的东西:
<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.Triggersyou would need to copy the default MyTextControlTemplateor else you would end up with no visual.
如果您想使用 ,则ControlTemplate.Triggers需要复制默认值MyTextControlTemplate,否则最终将没有视觉效果。
Aside from that I think the only difference is that Style.Triggershas a lower precedence than ControlTemplate.Triggers(Precedence documentation). But that would only matter if you use both trigger types.
除此之外,我认为唯一的区别是它Style.Triggers的优先级低于ControlTemplate.Triggers(优先文档)。但这只有在您同时使用两种触发器类型时才重要。

