为什么在 xaml、WPF 中的样式中定义模板?
自从我开始使用 MS 的控件模板示例作为构建自定义控件的基础以来,我一直想知道这一点。
以标签为例: http://msdn.microsoft.com/en- us/library/ms752327.aspx
到底为什么要这样定义:
<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>
然后直接作为模板调用而不是通过 style 属性调用?
做这样的事情有什么我看不到的隐藏原因吗?或者这只是做事的一种方式而已?
(注意:不要告诉我这是因为水平和垂直对齐设置器!我们都知道这些是标签的默认值,如果保留这些值,这基本上是无用的)
I've been wondering this ever since I started using MS's control templates examples as basis to build custom controls.
take the Label example for instance: 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>
and not like this directly:
<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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不使用样式,则无法自动将模板分配给特定控件类型的所有实例。为控件模板设置
x:Key="{x:Type Label}"
不会自动将此模板应用于Label类型的所有控件。您可以通过将
TargetType
设置为Button
来将样式应用于可视化树中声明下方的所有按钮,但您不能对模板执行相同的操作,如果您没有将其包装在具有模板的 Setter 的 Style 内。则可以
与
另请注意,在您的示例中,如果省略
x:Key
定义,x:Key
设置为TargetType
进行交换。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.You can make a style apply to all buttons below the declaration in the visual tree by setting the
TargetType
toButton
, 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
With
As the
x:Key
is set to theTargetType
if thex:Key
definition is omitted.