System.Windows.Controls 命名空 ...


.NET Framework 类库
ControlTemplate 类

更新:2007 年 11 月

指定 Control 的可在其多个实例之间共享的可视结构和行为方面。

命名空间:  System.Windows.Controls
程序集:  PresentationFramework(在 PresentationFramework.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/xaml/presentation

语法

Visual Basic(声明)
<LocalizabilityAttribute(LocalizationCategory.None, Readability := Readability.Unreadable)> _
Public Class ControlTemplate _
    Inherits FrameworkTemplate
Visual Basic (用法)
Dim instance As ControlTemplate
C#
[LocalizabilityAttribute(LocalizationCategory.None, Readability = Readability.Unreadable)]
public class ControlTemplate : FrameworkTemplate
Visual C++
[LocalizabilityAttribute(LocalizationCategory::None, Readability = Readability::Unreadable)]
public ref class ControlTemplate : public FrameworkTemplate
J#
/** @attribute LocalizabilityAttribute(LocalizationCategory.None, Readability = Readability.Unreadable) */
public class ControlTemplate extends FrameworkTemplate
JScript
public class ControlTemplate extends FrameworkTemplate
XAML 对象元素用法
<ControlTemplate>
  VisualTree
</ControlTemplate>
备注

ControlTemplate 允许您指定控件的可视结构。控件作者可以定义默认的 ControlTemplate,而应用程序作者可以重写 ControlTemplate 以重新构建控件的可视结构。

控件模板化是 WPF 样式和模板化模型提供的众多功能中的一种。样式和模板化模型为您提供了很大的灵活性,在许多情况下,您都不需要编写自己的控件。如果您是希望更改控件的可视化或替换现有控件的 ControlTemplate 的应用程序作者,请参见样式设置和模板化主题来获取示例和深入讨论。

如果您要编写自己的控件,请参见控件创作概述中的“创建自定义控件”。

ControlTemplate 旨在用作实现详细信息的独立单元,对于外部用户和对象(包括样式)不可见。仅可以从同一控件模板的内部操作此控件模板的内容。

示例

下面显示了一个 ButtonStyle,它用于设置 ButtonControlTemplate

C#
<Style TargetType="Button">
  <!--Set to true to not get any properties from the themes.-->
  <Setter Property="OverridesDefaultStyle" Value="True"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Grid>
          <Ellipse Fill="{TemplateBinding Background}"/>
          <ContentPresenter HorizontalAlignment="Center"
                            VerticalAlignment="Center"/>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

在应用它后,Button 显示为 Ellipse

按钮 ControlTemplate 示例

在将 ControlTemplate 属性设置为新 ControlTemplate 时(如上面的示例所示),将替换整个模板。Button 具有焦点或按下时的外观是将替换的按钮的默认外观的所有部分。因此,根据需要,您可能希望将按钮在按下时应有的外观等等纳入您的定义中,如下面的示例所示:

C#
<Style TargetType="Button">
  <Setter Property="SnapsToDevicePixels" Value="true"/>
  <Setter Property="OverridesDefaultStyle" Value="true"/>
  <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
  <Setter Property="MinHeight" Value="23"/>
  <Setter Property="MinWidth" Value="75"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Border 
          x:Name="Border"  
          CornerRadius="2" 
          BorderThickness="1"
          Background="{StaticResource NormalBrush}"
          BorderBrush="{StaticResource NormalBorderBrush}">
          <ContentPresenter 
            Margin="2"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            RecognizesAccessKey="True"/>
        </Border>
        <ControlTemplate.Triggers>
          <Trigger Property="IsKeyboardFocused" Value="true">
            <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DefaultedBorderBrush}" />
          </Trigger>
          <Trigger Property="IsDefaulted" Value="true">
            <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DefaultedBorderBrush}" />
          </Trigger>
          <Trigger Property="IsMouseOver" Value="true">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource DarkBrush}" />
          </Trigger>
          <Trigger Property="IsPressed" Value="true">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}" />
            <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource PressedBorderBrush}" />
          </Trigger>
          <Trigger Property="IsEnabled" Value="false">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
            <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

请注意,此示例引用了此处未显示的资源。有关完整示例,请参见使用 ControlTemplates 设置样式的示例。该示例提供了众多控件的控件模板示例,是创建控件模板的最佳入门方法。

继承层次结构

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.FrameworkTemplate
      System.Windows.Controls..::.ControlTemplate
线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
平台

Windows Vista

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

版本信息

.NET Framework

受以下版本支持:3.5、3.0
另请参见

参考

其他资源

标记 :


Page view tracker