Sdílet prostřednictvím


ControlTemplate.TargetType Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the type to which the ControlTemplate is applied.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property TargetType As Type
public Type TargetType { get; set; }
<ControlTemplate TargetType="typeName"/>

XAML Values

  • typeName
    A string that specifies the type name of the type where the template is applied.

Property Value

Type: System.Type
The type to which the ControlTemplate is applied.

Remarks

The Silverlight XAML processing behavior has special handling for property values that are of type Type. This behavior is to construct a Type instance as necessary based on its type name. You specify that name as the typeName attribute value as shown in the XAML syntax section. Note that {x:Type} is not supported in XAML for Silverlight 5 (you might have {x:Type} usages if you are migrating XAML). If you have {x:Type typeName} attribute values for TargetType, change them to just typeName.

Examples

The following example sets the TargetType property to Button to create a simple ControlTemplate for a Button.

<ControlTemplate TargetType="Button">
  <Grid >
    <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="CommonStates">

        <VisualStateGroup.Transitions>

          <!--Take one half second to trasition to the MouseOver state.-->
          <VisualTransition To="MouseOver" 
                              GeneratedDuration="0:0:0.5"/>
        </VisualStateGroup.Transitions>

        <VisualState x:Name="Normal" />

        <!--Change the SolidColorBrush, ButtonBrush, to red when the
            mouse is over the button.-->
        <VisualState x:Name="MouseOver">
          <Storyboard>
            <ColorAnimation Storyboard.TargetName="ButtonBrush" 
                            Storyboard.TargetProperty="Color" To="Red" />
          </Storyboard>
        </VisualState>
      </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Grid.Background>
      <SolidColorBrush x:Name="ButtonBrush" Color="Green"/>
    </Grid.Background>
  </Grid>
</ControlTemplate>

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.