开发者

Setting the XAML content of a control to a property of that control

I have a user 开发者_开发技巧control as such:

public partial class MyControl : UserControl
{
    private static readonly DependencyProperty pageContentProperty = DependencyProperty.Register("PageContent", typeof(UIElement), typeof(ActionPage), new PropertyMetadata(null));

    public UIElement PageContent
    {
        get { return (UIElement)base.GetValue(pageContentProperty); }
        set { base.SetValue(pageContentProperty, value); }
    }

    public MyControl()
    {
        InitializeComponent();
    }

    // More code
}

Now if I use it in XAML I have to do:

<l:MyControl>
    <l:MyControl.PageContent>
         <TextBlock Text="Lorum Ipsum"/>
    </l:MyControl.PageContent>
</l:MyControl>

But I want to just do:

<l:MyControl>
    <TextBlock Text="Lorum Ipsum"/>
</l:MyControl>

Currently if I do this it replace the entire content of the control with the TextBlock (which makes sense for a normal UserControl) but how can I override this behavior?


You might try adding the ContentProperty attribute to your PageContent property:-

[ContentProperty("PageContent")]
public partial class MyControl : UserControl
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜