ControlTemplate.TargetType 属性

定义

获取或设置此 ControlTemplate 所针对的类型。

public:
 property Type ^ TargetType { Type ^ get(); void set(Type ^ value); };
[System.Windows.Markup.Ambient]
public Type TargetType { get; set; }
[<System.Windows.Markup.Ambient>]
member this.TargetType : Type with get, set
Public Property TargetType As Type

属性值

Type

默认值是 null

属性

例外

如果模板定义中包含 TargetType,则 ContentPresenter 属性不能为 null

指定的类型无效。 TargetTypeControlTemplate 必须是 ControlPagePageFunctionBase 或从其继承。

示例

以下示例演示了此属性的使用:

<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>

上面的示例使用以下资源:

有关完整示例,请参阅使用 ControlTemplates 设置样式示例

注解

如果在资源部分中TargetType具有一个独立ControlTemplate属性设置为某个类型,ControlTemplate则不会自动应用于该类型。 相反,需要显式指定 x:Key 并应用模板。

另请注意,TargetType如果模板定义包含一个ContentPresenter属性,则需要ControlTemplate该属性。

XAML 属性用法

<object  TargetType="typeName"/>  

XAML 值

typeName
类的类型名称。 若要引用 Type 类的名称,请使用 标记扩展和 WPF XAML

适用于