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
type ContentPropertyAttribute = class
    inherit Attribute
Public NotInheritable Class ContentPropertyAttribute
Inherits Attribute
繼承
ContentPropertyAttribute
屬性

範例

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

[ContentProperty("Title")]
public class Film
{
    public Film()
    {
    }

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

    private string _title;
}
<ContentProperty("Title")>
Public Class Film
    Public Sub New()
    End Sub

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

    Private _title As String
End Class

備註

如果 ContentPropertyAttribute 的相關聯屬性不屬於類型 stringobject,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 屬性屬性的宣告(並注意類別沒有內容屬性)。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 會以新的 ContentPropertyAttribute 取代繼承的。Applying 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 的類別範例是 ContentControlButton 類別會繼承自。An example of a class in Windows Presentation Foundation (WPF) that uses the ContentPropertyAttribute is ContentControl, which the Button class inherits from. ContentControl.Content 屬性是由 ContentPropertyAttribute所設定的 content 屬性。The ContentControl.Content property is the content property set by the ContentPropertyAttribute. 如果 Button 在 XAML 中具現化,ButtonContent 將會設定為在 [開始] 和 [結束] 按鈕標記之間的元素。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()

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

ContentPropertyAttribute(String)

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

屬性

Name

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

TypeId

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

(繼承來源 Attribute)

方法

Equals(Object)

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

(繼承來源 Attribute)
GetHashCode()

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

(繼承來源 Attribute)
GetType()

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

(繼承來源 Object)
IsDefaultAttribute()

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

(繼承來源 Attribute)
Match(Object)

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

(繼承來源 Attribute)
MemberwiseClone()

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

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。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)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (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)

適用於

另請參閱