WPF - UserControl default Content attribute

陌路散爱 提交于 2021-02-05 20:22:52

问题


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.


回答1:


ContentPropertyAttribute




回答2:


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>


来源:https://stackoverflow.com/questions/661620/wpf-usercontrol-default-content-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!