匯出 (0) 列印
全部展開
EN
本內容沒有您的語言版本,但有英文版本。

Style.TargetType Property

July 28, 2014

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

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

public Type TargetType { get; set; }
<Style TargetType="typeName"/>

XAML Values

typeName

A string that specifies the type name of the type where the style is applied.

Property Value

Type: System.Type
The type of object to which the style is applied.

You must set the TargetType property when you create a Style. If you do not, an exception is thrown.

You can set a style on any element that derives from FrameworkElement. Therefore, the target type can be any of those elements.

The XAML processing behavior has special handling for property values that are of type Type. This behavior is to construct a Type as necessary, based on its type name, evaluated against the types that are mapped into XAML namespaces. You specify the evaluated type name as the typeName attribute value as shown in the XAML syntax section.

The following example creates two styles: one for a TextBlock and one for a TextBox. Each style is applied to two instances of a control to create a uniform appearance for each TextBlock and TextBox. The example sets the FrameworkElement.Style property of each control by referencing the Style as a StaticResource.


<StackPanel>
  <StackPanel.Resources>
      <!--Create a Style for a TextBlock that uses some 
          values from the Theme Resources for Windows Phone 
          (http://msdn.microsoft.com/en-us/library/ff769552(v=VS.92).aspx).-->
      <Style TargetType="TextBlock" x:Key="TextBlockStyle">
              <Setter Property="Foreground" Value="{StaticResource PhoneSubtleColor}"/>
              <Setter Property="Margin" Value="{StaticResource PhoneMargin}"/>
              <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
      </Style>

      <!--Create a Style for a TextBox that uses some 
          values from the Theme Resources for Windows Phone 
          (http://msdn.microsoft.com/en-us/library/ff769552(v=VS.92).aspx).-->
      <Style TargetType="TextBox" x:Key="TextBoxStyle">
          <Setter Property="Width" Value="300"/>
          <Setter Property="Height" Value="70"/>
          <Setter Property="Margin" Value="{StaticResource PhoneMargin}"/>
          <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
          <Setter Property="Background" Value="{StaticResource PhoneAccentColor}" />
    </Style>
  </StackPanel.Resources>

  <!--Apply the TextBlockStyle and TextBoxStyle to each 
          TextBlock and TextBox, respectively.-->
  <StackPanel Orientation="Horizontal">
    <TextBlock Style="{StaticResource TextBlockStyle}">
              First Name:
          </TextBlock>
    <TextBox Style="{StaticResource TextBoxStyle}"/>
  </StackPanel>
  <StackPanel Orientation="Horizontal">
    <TextBlock Style="{StaticResource TextBlockStyle}">
              Last Name:
          </TextBlock>
    <TextBox Style="{StaticResource TextBoxStyle}" />
  </StackPanel>
</StackPanel>


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

顯示:
© 2014 Microsoft