Export (0) Print
Expand All

ControlTemplate.TargetType Property

Gets or sets the type for which this ControlTemplate is intended.

Namespace: System.Windows.Controls
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public Type TargetType { get; set; }
/** @property */
public Type get_TargetType ()

/** @property */
public void set_TargetType (Type value)

public function get TargetType () : Type

public function set TargetType (value : Type)

<object TargetType="typeName"/>
 typeName  The type name of the class. To refer to the Type name of the class, use the XAML-defined markup extension.  

Property Value

The default value is a null reference (Nothing in Visual Basic).

Exception typeCondition

ArgumentNullException

The TargetType property must not be a null reference (Nothing in Visual Basic) if the definition of the template has a ContentPresenter.

ArgumentException

The specified types is not valid. The TargetType of a ControlTemplate must be or inherit from a Control, a Page, or a PageFunctionBase.

If you have a standalone ControlTemplate in the resources section with the TargetType property set to a type, the ControlTemplate does not get applied to that type automatically. Instead, you need to specify an x:Key and apply the template explicitly.

Also note that the TargetType property is required on a ControlTemplate if the template definition contains a ContentPresenter.

The following example demonstrates the use of this property:

<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"
                    Value="{StaticResource DisabledForegroundBrush}"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

The above example uses the following resource:

<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />

For the complete sample, see Styling with ControlTemplates Sample.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2015 Microsoft