ContentPropertyAttribute ContentPropertyAttribute ContentPropertyAttribute Class

Definition

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 : sealed class ContentPropertyAttribute : Attribute
public sealed class ContentPropertyAttribute : Attribute
Public NotInheritable Class ContentPropertyAttribute Inherits Attribute
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

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: ```xaml <StackPanel.Children> Testing content attribute </StackPanel.Children>




The above is equivalent to this more readable XAML:
```xaml
<StackPanel>
  <TextBlock>Testing content attribute</TextBlock>
</StackPanel>

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.

Constructors

ContentPropertyAttribute() ContentPropertyAttribute() ContentPropertyAttribute()

Initializes a new instance of the ContentPropertyAttribute class.

public : ContentPropertyAttribute()
public ContentPropertyAttribute()
Public Sub New()

See Also