ContentPropertyAttribute 類別

定義

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

public ref class ContentPropertyAttribute sealed : Attribute
[Windows.Foundation.Metadata.AttributeUsage(System.AttributeTargets.RuntimeClass)]
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
public sealed class ContentPropertyAttribute : Attribute
Public NotInheritable Class ContentPropertyAttribute
Inherits Attribute
繼承
ContentPropertyAttribute
屬性

備註

當 XAML 剖析器和其他 XAML 架構程式碼解譯時,此屬性是用來識別屬性型別的特定屬性應該視為 XAML 內容屬性。 XAML 內容屬性的用途是它提供 XAML 語法速記,可省略該屬性的屬性專案標記。 移除屬性元素有助於在 XAML 標記中產生更自然的父子系形式。 For more info on XAML content properties, see "XAML content properties" section of XAML syntax guide.

預設Windows 執行階段 XAML 詞彙中已套用 ContentPropertyAttribute 的類別範例是PanelPanel上的屬性 Children會識別為由 ContentPropertyAttribute 及其Name值所定義的 XAML 內容屬性。 內容屬性資訊會由所有衍生的 Panel類型繼承,例如 GridCanvasStackPanel

此程式碼大約會在 C# 中套用 ContentPropertyAttribute 的方式, (在原生程式碼中套用 true 定義,這僅適用于僅) :

[ContentProperty(Name = "Children")]
    public class Panel : FrameworkElement
    { ...}

使用 Panel 及其衍生類別 StackPanel 做為 XAML 內容屬性概念的圖例,您可能會有下列 XAML:

<StackPanel>
  <StackPanel.Children>
    <TextBlock>Testing content attribute</TextBlock>
  </StackPanel.Children>
</StackPanel>

上述相當於這個更容易閱讀的 XAML:

<StackPanel>
  <TextBlock>Testing content attribute</TextBlock>
</StackPanel>

由 XAML 剖析器剖析時,剖析器會透過在 Panel上套用的 ContentPropertyAttribute 知道在 StackPanel標籤主體中找到的任何內容都應該用來設定所建立StackPanel實例上的Children值。

您可以在 TextBlock類別上看到 ContentPropertyAttribute 的另一個醒目範例。 TextBlock 使用 Inlines 做為其內容屬性,而預設內嵌類別 Run 會使用 Text 做為其內容屬性。 結合在一起,內容屬性會啟用簡單的內嵌語法,例如 <TextBlock>Hello</TextBlock> ,即使這個 XAML 所建立的物件圖形更為複雜,也能夠視需要支援多個明確的內嵌專案。

建構函式

ContentPropertyAttribute()

初始化 ContentPropertyAttribute 類別的新實例。

欄位

Name

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

適用於

另請參閱