问题
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