Dela via


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 ref class ContentPropertyAttribute sealed : Attribute
/// [Windows.Foundation.Metadata.AttributeUsage(System.AttributeTargets.RuntimeClass)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
class ContentPropertyAttribute final : Attribute
/// [Windows.Foundation.Metadata.AttributeUsage(System.AttributeTargets.RuntimeClass)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.AttributeName("contentproperty")]
class ContentPropertyAttribute final : Attribute
[Windows.Foundation.Metadata.AttributeUsage(System.AttributeTargets.RuntimeClass)]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
public sealed class ContentPropertyAttribute : Attribute
[Windows.Foundation.Metadata.AttributeUsage(System.AttributeTargets.RuntimeClass)]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.AttributeName("contentproperty")]
public sealed class ContentPropertyAttribute : Attribute
Public NotInheritable Class ContentPropertyAttribute
Inherits Attribute
Inheritance
ContentPropertyAttribute
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Remarks

C++/WinRT. See The [contentproperty] attribute.

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:

<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.

Constructors

ContentPropertyAttribute()

Initializes a new instance of the ContentPropertyAttribute class.

Fields

Name

Applies to

See also