ContentPropertyAttribute Class


Indicates which property of a type is the XAML content property. A XAML processor uses this information when processing XAML child elements of XAML representations of the attributed type.

public ref class ContentPropertyAttribute sealed : Attribute
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
public sealed class ContentPropertyAttribute : Attribute
Public NotInheritable Class ContentPropertyAttribute
Inherits Attribute
Windows.Foundation.Metadata.AttributeUsageAttribute Windows.Foundation.Metadata.ContractVersionAttribute


This attribute is used to identify that a specific property of the attributed type should be considered the XAML content property, when interpreted by XAML parsers and other XAML framework code. The purpose of a XAML content property is that it provides a XAML syntax shorthand that can omit property element markup for that property. The removal of property elements facilitates a more natural parent-children form in XAML markup. For more info on XAML content properties, see "XAML content properties" section of XAML syntax guide.

An example of a class in the default Windows Runtime XAML vocabulary that has ContentPropertyAttribute applied is Panel. The property Children on the Panel is identified as the XAML content property as defined by the ContentPropertyAttribute and its Name value. The content property information is inherited by all derived types of Panel, such as Grid and Canvas and StackPanel.

This code approximates how Panel applies ContentPropertyAttribute in C# (the true definition is applied in native code, this is for illustration only):

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

Using Panel and its derived class StackPanel as the illustration of the XAML content property concept, you may have the following XAML:

    <TextBlock>Testing content attribute</TextBlock>

The above is equivalent to this more readable XAML:

  <TextBlock>Testing content attribute</TextBlock>

When parsed by a XAML parser, the parser knows through the ContentPropertyAttribute applied on Panel that any content found within the body of a StackPanel tag should be used to set the value of Children on the created StackPanel instance.

Another prominent example of ContentPropertyAttribute in action can be seen on the TextBlock class. TextBlock uses Inlines as its content property, and the default inline class Run uses Text as its content property. In combination, the content properties enable a simple inline syntax such as <TextBlock>Hello</TextBlock>, even though the object graph created by this XAML is more complex and is also capable of supporting multiple explicit inline elements if desired.



Initializes a new instance of the ContentPropertyAttribute class.



Applies to

See also