如何在WPF中为自定义控件提供两个默认模板?

时间:2022-09-02 13:33:35

In Charles Petzold's "Using Templates to Customize WPF Controls" article in the Jan 2007 edition of MSDN Magazine (http://msdn.microsoft.com/en-us/magazine/cc163497.aspx), he says,

在Charles Petzold的2007年1月版MSDN杂志(http://msdn.microsoft.com/en-us/magazine/cc163497.aspx)中使用“使用模板自定义WPF控件”一文时,他说,

The ProgressBar control actually has two default templates for the two orientations. (This is true of ScrollBar and Slider, as well.) If you want your new ProgressBar to support both orientations, you should write two separate templates and select them in the Triggers section of a Style element that you also define for the ProgressBar.

ProgressBar控件实际上有两个默认模板用于两个方向。 (对于ScrollBar和Slider也是如此。)如果您希望新的ProgressBar支持两种方向,则应编写两个单独的模板,并在Style元素的Triggers部分中选择它们,您也可以为ProgressBar定义它们。

I am currently writing a custom control that requires this functionality, but can't work out how to do as he says - not in any way that works, anyhow. Does anybody have a sample of this?

我目前正在编写一个需要此功能的自定义控件,但无法解决如何操作的问题 - 无论如何不能以任何方式工作。有没有人有这样的样品?

Thanks in advance.

提前致谢。

1 个解决方案

#1


You can see how its done in the scrollbar sample control template http://msdn.microsoft.com/en-us/library/ms742173.aspx

您可以在滚动条示例控件模板中看到它是如何完成的http://msdn.microsoft.com/en-us/library/ms742173.aspx

I copied only the relevant parts of the sample here.

我在这里只复制了样本的相关部分。

In short there are two templates in the resource dictionary:

简而言之,资源字典中有两个模板:

<ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}"> ...

<ControlTemplate x:Key="HorizontalScrollBar" TargetType="{x:Type ScrollBar}"> ...

And a trigger in the style to switch between them :

并且风格中的触发器可以在它们之间切换:

<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
  <Style.Triggers>
    <Trigger Property="Orientation" Value="Horizontal">
      <Setter Property="Template" Value="{StaticResource HorizontalScrollBar}" />
    </Trigger>
    <Trigger Property="Orientation" Value="Vertical">
      <Setter Property="Template" Value="{StaticResource VerticalScrollBar}" />
    </Trigger>
  </Style.Triggers>
</Style>

#1


You can see how its done in the scrollbar sample control template http://msdn.microsoft.com/en-us/library/ms742173.aspx

您可以在滚动条示例控件模板中看到它是如何完成的http://msdn.microsoft.com/en-us/library/ms742173.aspx

I copied only the relevant parts of the sample here.

我在这里只复制了样本的相关部分。

In short there are two templates in the resource dictionary:

简而言之,资源字典中有两个模板:

<ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}"> ...

<ControlTemplate x:Key="HorizontalScrollBar" TargetType="{x:Type ScrollBar}"> ...

And a trigger in the style to switch between them :

并且风格中的触发器可以在它们之间切换:

<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
  <Style.Triggers>
    <Trigger Property="Orientation" Value="Horizontal">
      <Setter Property="Template" Value="{StaticResource HorizontalScrollBar}" />
    </Trigger>
    <Trigger Property="Orientation" Value="Vertical">
      <Setter Property="Template" Value="{StaticResource VerticalScrollBar}" />
    </Trigger>
  </Style.Triggers>
</Style>