ContentPropertyAttribute 類別


指出類型的哪一個屬性是 XAML 內容屬性。Indicates which property of a type is the XAML content property. XAML 處理器會在處理屬性化類型之 XAML 表示的 XAML 子項目時,使用這項資訊。A XAML processor uses this information when processing XAML child elements of XAML representations of the attributed type.

public ref class ContentPropertyAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
public sealed class ContentPropertyAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=true)>]
type ContentPropertyAttribute = class
    inherit Attribute
Public NotInheritable Class ContentPropertyAttribute
Inherits Attribute


下列範例會建立名為且已套用的類別 Film ContentPropertyAttributeThe following example creates a class named Film that has a ContentPropertyAttribute applied. 名為的屬性 Title 會指定為 content 屬性。The property named Title is indicated as the content property.

public class Film
    public Film()

    public string Title
        get { return _title; }
        set { _title = value; }

    private string _title;
Public Class Film
    Public Sub New()
    End Sub

    Public Property Title() As String
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    Private _title As String
End Class


如果的關聯屬性 ContentPropertyAttribute 不是 string 或類型 object ,XAML 處理器會嘗試識別值轉換技術。If the associated property of a ContentPropertyAttribute is not of type string or object, a XAML processor attempts to identify a value conversion technique. 第一個檢查是用於原生類型轉換,也就是 XAML 語言基本類型或該特定 XAML 寫入器執行所啟用的特定原生轉換。The first check is for native type conversion, either of the XAML language primitives or of specific native conversions enabled by that particular XAML writer implementation. 下一步是尋找類型轉換器。The next step is to look for a type converter. 在 .NET 的執行中,類型轉換器是根據套用的 TypeConverterAttribute 成員層級或型別層級定義來識別。In the .NET implementations, a type converter is identified based on TypeConverterAttribute on either the member level or type level definition that applies. 如果無法識別任何值轉換,XAML 物件寫入器通常會擲回例外狀況。If no value conversion can be identified, a XAML object writer typically throws an exception.

為了接受多個單一物件元素作為內容,XAML 內容屬性的類型必須是可支援的集合類型。In order to accept more than a single object element as content, the type of the XAML content property must be supportable as a collection type.

由於屬性的宣告 Inherited=true ,的值 ContentPropertyAttribute 通常也會將內容屬性指定套用至所有衍生的類別。Because of the Inherited=true declaration of the attribute, a value for a ContentPropertyAttribute normally applies a content property designation to all derived classes as well. 套用空的 ContentPropertyAttribute 可讓衍生類別以基類 (移除 content 屬性屬性的宣告,並注意類別沒有任何 content 屬性) 。Applying an empty ContentPropertyAttribute enables a derived class to remove a declaration of a content property attribute by a base class (and to note that the class has no content property). 套用以不同方式命名 ContentPropertyAttribute 的,會將繼承的取代為新的 ContentPropertyAttributeApplying a differently named ContentPropertyAttribute replaces the inherited ContentPropertyAttribute with the new one.

在舊版的 .NET Framework 中,這個類別會存在於 WPF 特定的元件 WindowsBase 中,而且也會在 Windows Communication Foundation (WCF) 中具有平行執行。In previous versions of the .NET Framework, this class existed in the WPF-specific assembly WindowsBase, and also had a parallel implementation in Windows Communication Foundation (WCF). 從 .NET Framework 4.0 開始, ContentPropertyAttribute 位於 system.object 元件中。Starting with the .NET Framework 4.0, ContentPropertyAttribute is in the System.Xaml assembly. 如需詳細資訊,請參閱 Types Migrated from WPF to System.XamlFor more information, see Types Migrated from WPF to System.Xaml.

WPF 使用注意事項WPF Usage Notes

Windows Presentation Foundation 中 (WPF) 的範例,該類別 ContentPropertyAttribute 會使用 ContentControl Button 類別繼承自。An example of a class in Windows Presentation Foundation (WPF) that uses the ContentPropertyAttribute is ContentControl, which the Button class inherits from. ContentControl.Content屬性是由設定的 content 屬性 ContentPropertyAttributeThe ContentControl.Content property is the content property set by the ContentPropertyAttribute. 如果 Button 是在 XAML 中具現化,的將會 Content Button 設定為開始與結束按鈕標記之間的元素。If a Button is instantiated in XAML, the Content of the Button will be set to the element that is between the start and end button tags.



初始化 ContentPropertyAttribute 類別的新執行個體。Initializes a new instance of the ContentPropertyAttribute class.


使用指定的名稱,初始化 ContentPropertyAttribute 類別的新執行個體。Initializes a new instance of the ContentPropertyAttribute class, by using the specified name.



取得表示內容屬性之屬性的名稱。Gets the name of the property that is the content property.


在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)



傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(繼承來源 Attribute)

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)


_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(繼承來源 Attribute)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)