Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Style.TargetType Property

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

Namespace: System.Windows
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:

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 target type for this style.

You can set a style on any element that derives from FrameworkElement or FrameworkContentElement. Therefore, your target type can be any of those elements. However, if you create a style with a TargetType property and base it on another style that also defines a TargetType property, the target type of the derived style must be the same as or be derived from the target type of the base style.

If you do not specify a TargetType, then you must specify an x:Key for your Style and also qualify the properties in your style with a class name.

The following example defines a style that will be applied to all instances of the TextBlock element.

<Style TargetType="{x:Type TextBlock}">
  <Setter Property="FontFamily" Value="Segoe Black" />
  <Setter Property="HorizontalAlignment" Value="Center" />
  <Setter Property="FontSize" Value="12pt" />
  <Setter Property="Foreground" Value="#777777" />

Setting the TargetType property to the TextBlock type without setting an x:Key implicitly sets the x:Key to {x:Type TextBlock}. This also means that if you give the above Style an x:Key value of anything other than {x:Type TextBlock}, the Style would not be applied to all TextBlock elements automatically. Instead, you need to apply the style to the TextBlock elements explicitly.

Many WPF controls consist of a combination of other WPF controls, so creating a style that applies to all controls of a type can have broad impact. For instance, if you create a style that targets the TextBox controls in a Canvas, the style is applied to all TextBox controls in the canvas, even if the TextBox is part of another control, such as a ComboBox.

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
© 2018 Microsoft