Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - UserControl default Content attribute

I'm creating a UserControl and I just can't remember the name of the attribute which you use to decorate the property which you want to act as the default content property.

To give a concrete example, say i have a property called 'Title' which i can set using property syntax like this -

<local:myControl Title="the title"/>

But the consumer of the control may want to use element syntax like this -

<local:myControl> the Title </local:myControl>

I KNOW there is an attribute which I need to add to the Title property with to enable this support but I've forgotten what it is and can't find it anywhere.

Could anyone refresh my memory for me? Also, I'm looking for a similar attribute to act on CustomControls inheriting from ItemsControl.

like image 842
EightyOne Unite Avatar asked Mar 19 '09 09:03

EightyOne Unite


2 Answers

ContentPropertyAttribute

like image 127
Kent Boogaart Avatar answered Nov 06 '22 02:11

Kent Boogaart


I also found the code for supporting collections as the content property on MSDN. TOM_C is to thank for this.

[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>
like image 7
EightyOne Unite Avatar answered Nov 06 '22 02:11

EightyOne Unite