System.Windows.Markup Names ...


.NET Framework Class Library
ContentPropertyAttribute Class

Specifies which property of a class to use as the content property when written as XAML.

Namespace:  System.Windows.Markup
Assembly:  WindowsBase (in WindowsBase.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml
Syntax

Visual Basic (Declaration)
<AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple := False, Inherited := True)> _
Public NotInheritable Class ContentPropertyAttribute _
    Inherits Attribute
Visual Basic (Usage)
Dim instance As ContentPropertyAttribute
C#
[AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public sealed class ContentPropertyAttribute : Attribute
Visual C++
[AttributeUsageAttribute(AttributeTargets::Class, AllowMultiple = false, Inherited = true)]
public ref class ContentPropertyAttribute sealed : public Attribute
JScript
public final class ContentPropertyAttribute extends Attribute
XAML

This managed class is not typically used in XAML.

Remarks

If the associated property of a ContentPropertyAttribute is not of type string or object, a type converter will be called at runtime. If a type converter cannot be found at runtime, an exception is thrown.

In order to accept more than a single object element as content, the type of the content property must be a collection type.

An example of a class in Windows Presentation Foundation (WPF) that uses the ContentPropertyAttribute is ContentControl, which the Button class inherits from. The property Content on the ContentControl is the content property set by the ContentPropertyAttribute. If a Button is instantiate in XAML, Content of the Button will be set to the element that is between the start and end button tags.

Examples

The following example creates a class named Film which is decorated with the ContentPropertyAttribute. The property named Title is set as the content property.

C#
[ContentProperty("Title")]
public class Film
{
    public Film()
    {
    }

    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    private string _title;
}
Inheritance Hierarchy

System..::.Object
  System..::.Attribute
    System.Windows.Markup..::.ContentPropertyAttribute
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Other Resources

Tags :


Community Content

Aquatique
collection type as content
When implementing a content property of a collection type, the following information can be helpful:

Make sure only the getter of your property is made public.
  • By doing so, you don't have to provide an entire collection as content, instead you can directly add the children to the content.
Example:

C#:

[ContentProperty("SomeObjects")]
public class SomeContainer
{

...

private List<SomeObject> _someObjects;

public List<SomeObject> SomeObjects
{
get
{
if (null == _someObjects)
{
_someObjects = new List<SomeObject>();
}

return _someObjects;
}
}

XAML:
<SomeContainer>
<SomeObject/>
<SomeObject/>
<SomeObject/>
</SomeContainer>


Pablo_Marambio
About collections in content
I would like to add a point to the prior comment.

If the idea is to present the control content (in this case a collection) directly in the screen, then the simplest approach would be to declare the content property as UIElementCollection and make it expose the Children property of some of the user control's control.

For example, if the user control has a stack panel named "stack", the control property could look like this:
[ContentProperty("MyContent")]
class MyUserControl : UserControl {
...

public UIElementCollection MyContent {
get {
return this.stack.Children;
}
//no need for set property, as the previous coment says
}

Page view tracker