ContentPropertyAttribute attribute

Applies to Windows and Windows Phone

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.

Inheritance

Object
  Attribute
    ContentPropertyAttribute

Syntax


public ref class ContentPropertyAttribute sealed : Attribute

Attributes

[AttributeUsage(512)]
[Version(0x6020000)]

Members

The ContentPropertyAttribute attribute has these types of members:

Constructors

The ContentPropertyAttribute attribute has these constructors.

ConstructorDescription
ContentPropertyAttribute Initializes a new instance of the ContentPropertyAttribute class.

 

Fields

The ContentPropertyAttribute attribute has these fields.

FieldData typeDescription
Name

System.String [.NET] | Platform::String [C++]

Gets the name of the property that is the declared content property.

 

Methods

The ContentPropertyAttribute attribute inherits methods from the Object class.

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 Basic 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:


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

The above is equivalent to this more readable 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.

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml.Markup
Windows::UI::Xaml::Markup [C++]

Metadata

Windows.winmd

See also

Attribute
Basic XAML syntax guide
XAML overview

 

 

Show:
© 2014 Microsoft