为什么在 xaml、WPF 中的 Style 中定义模板? [英] Why define a Template inside a Style in xaml, WPF?
问题描述
自从我开始使用 MS 的控件模板示例作为构建自定义控件的基础以来,我一直在想这个问题.
I've been wondering this ever since I started using MS's control templates examples as basis to build custom controls.
以标签为例:http://msdn.microsoft.com/en-us/library/ms752327.aspx
到底为什么是这样定义的:
why on earth is it defined like this:
<Style x:Key="{x:Type Label}" TargetType="Label">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="True" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource DisabledForegroundColor}" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
而不是直接这样:
<ControlTemplate x:Key="{x:Type Label}" TargetType="Label">
<Border>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="True" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource DisabledForegroundColor}" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
然后直接作为模板调用而不是通过样式属性?
and then called as a template directly and not through the style property?
我不认为这样做有什么隐藏的原因吗?或者这只是一种做事方式,仅此而已?
is there a hidden reason I do not see for doing things like this? or is it just one way of doing things and that's it?
(注意:不要告诉我这是因为水平和垂直对齐设置器!我们都知道这些是标签的默认值,如果你保留这些值,这基本上没用)
(NB: don't tell me this is because of the horizontal and vertical alignment setters! we all know those are the default values for a label and this is basically useless if you keep those values)
推荐答案
如果不使用样式,就不可能自动将模板分配给特定控件类型的所有实例.为控件模板设置x:Key="{x:Type Label}"
不会自动将此模板应用于Label类型的所有控件.
Without using a Style it's not possible to automatically assign the template to all instances of a specific control type. Setting x:Key="{x:Type Label}"
for the control template does not automatically apply this template to all controls of type Label.
您可以通过将 TargetType
设置为 Button
来将样式应用于可视化树中声明下方的所有按钮,但是您不能使用模板,如果您没有将其包装在具有 Setter 模板的 Style 中.
You can make a style apply to all buttons below the declaration in the visual tree by setting the TargetType
to Button
, but you can't do the same with a template, if you do not wrap it inside a Style that have a Setter for the template.
另外,请注意,在您的示例中,您可以交换
Also, note that in your example you can exchange
<Style x:Key="{x:Type Label}" TargetType="Label">
与
<Style TargetType="Label">
如果 x:Key
定义被省略,x:Key
被设置为 TargetType
.
As the x:Key
is set to the TargetType
if the x:Key
definition is omitted.
这篇关于为什么在 xaml、WPF 中的 Style 中定义模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!