다음을 통해 공유


TemplatePartAttribute Class

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

Represents an attribute that is applied to the class definition to identify the types of the named parts that are used for control templating.

Inheritance Hierarchy

System.Object
  System.Attribute
    System.Windows.TemplatePartAttribute

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

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple := True)> _
Public NotInheritable Class TemplatePartAttribute _
    Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = true)]
public sealed class TemplatePartAttribute : Attribute

The TemplatePartAttribute type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows Phone TemplatePartAttribute Initializes a new instance of the TemplatePartAttribute class

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows Phone Name Gets or sets a string that reports the object model name of a template part.
Public propertySupported by Silverlight for Windows Phone Type Gets or sets a string that reports the Type of a template part.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows Phone Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

Control authors apply this attribute to the class definition of a control class to inform template authors what elements the control expects to find in its ControlTemplate. These parts must have the name specified by the Name property in order to properly interact with the control's constructor and run-time logic. There can only be one element with a given name in any template.

Examples

The following example shows TemplatePartAttribute objects that are defined on the definition of the ComboBox class.

<TemplatePartAttribute(Name:="ContentPresenter", Type:=GetType(ContentPresenter))> _
<TemplatePartAttribute(Name:="Popup", Type:=GetType(Popup))> _
Public Class ComboBox
    Inherits ItemsControl

End Class
[TemplatePartAttribute(Name = "ContentPresenter", Type = typeof(ContentPresenter))]
[TemplatePartAttribute(Name = "Popup", Type = typeof(Popup))]
public class ComboBox : ItemsControl
{
}

The following example shows a simplified ControlTemplate for the ComboBox that includes the elements that are specified by the TemplatePartAttribute objects on the ComboBox class.

<ControlTemplate TargetType="ComboBox">
  <Grid>
    <Border x:Name="ContentPresenterBorder">
      <Grid>
        <ToggleButton x:Name="DropDownToggle"
                      HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  
                      Margin="-1" HorizontalContentAlignment="Right">
          <Path x:Name="BtnArrow" Height="4" Width="8" 
                Stretch="Uniform" Margin="0,0,6,0"  Fill="Black"
                Data="F1 M 300,-190L 310,-190L 305,-183L 301,-190 Z " />
        </ToggleButton>
        <ContentPresenter x:Name="ContentPresenter" Margin="6,2,25,2">
          <TextBlock Text=" " />
        </ContentPresenter>
      </Grid>
    </Border>
    <Popup x:Name="Popup">
      <Border x:Name="PopupBorder" 
              HorizontalAlignment="Stretch" Height="Auto" 
              BorderThickness="{TemplateBinding BorderThickness}" 
              BorderBrush="Black" Background="White" CornerRadius="3">
        <ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1">
          <ItemsPresenter/>
        </ScrollViewer>
      </Border>
    </Popup>

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

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference