为什么在 xaml、WPF 中的样式中定义模板?

发布于 2024-10-15 21:40:00 字数 2287 浏览 3 评论 0原文

自从我开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殊姿 2024-10-22 21:40:00

如果不使用样式,则无法自动将模板分配给特定控件类型的所有实例。为控件模板设置x:Key="{x:Type Label}"不会自动将此模板应用于Label类型的所有控件。

您可以通过将 TargetType 设置为 Button 来将样式应用于可视化树中声明下方的所有按钮,但您不能对模板执行相同的操作,如果您没有将其包装在具有模板的 SetterStyle 内。

则可以

<Style x:Key="{x:Type Label}" TargetType="Label">

<Style TargetType="Label">

另请注意,在您的示例中,如果省略 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 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">

With

<Style TargetType="Label">

As the x:Key is set to the TargetType if the x:Key definition is omitted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文