ContentPropertyAttribute 類別

定義

指出類型的哪一個屬性是 XAML 內容屬性。 XAML 處理器會在處理屬性化類型之 XAML 表示的 XAML 子項目時,使用這項資訊。

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
繼承
ContentPropertyAttribute
屬性

範例

下列範例會建立名為 Film 且已套用 的 ContentPropertyAttribute 類別。 名為 Title 的屬性會以內容屬性表示。

[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 處理器會嘗試識別值轉換技術。 第一個檢查是原生類型轉換,可能是 XAML 語言基本類型或該特定 XAML 寫入器實作所啟用的特定原生轉換。 下一個步驟是尋找類型轉換器。 在 .NET 實作中,會根據 TypeConverterAttribute 套用的成員層級或類型層級定義來識別類型轉換器。 如果無法識別任何值轉換,XAML 物件寫入器通常會擲回例外狀況。

若要接受多個單一物件專案做為內容,XAML 內容屬性的類型必須可做為集合類型支援。

Inherited=true由於屬性的宣告,ContentPropertyAttribute一般會將內容屬性指定套用至所有衍生類別的值。 套用空白 ContentPropertyAttribute 可讓衍生類別移除基類屬性的宣告 (,並請注意類別沒有內容屬性) 。 以不同的方式套用名為 ContentPropertyAttribute 的 ,會以新名稱取代繼承 ContentPropertyAttribute 的 。

在舊版的 .NET Framework 中,這個類別存在於 WPF 特定的元件 WindowsBase 中,而且在 Windows Communication Foundation (WCF) 中也有平行實作。 從 .NET Framework 4.0 開始,ContentPropertyAttribute位於 System.Xaml 元件中。 如需詳細資訊,請參閱 Types Migrated from WPF to System.Xaml

WPF 使用注意事項

Windows Presentation Foundation (WPF) 中使用 ContentPropertyAttribute 的 類別範例是 ContentControl,類別Button繼承自此類別。 屬性 ContentControl.Content 是由 所設定 ContentPropertyAttribute的內容屬性。 Button如果在 XAML 中具現化 ,ContentButton 將會設定為開始和結束按鈕標記之間的專案。

建構函式

ContentPropertyAttribute()

初始化 ContentPropertyAttribute 類別的新執行個體。

ContentPropertyAttribute(String)

使用指定的名稱,初始化 ContentPropertyAttribute 類別的新執行個體。

屬性

Name

取得表示內容屬性之屬性的名稱。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

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

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

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

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱