C# 将控件的XAML内容设置为该控件的属性

C# 将控件的XAML内容设置为该控件的属性,c#,silverlight,xaml,C#,Silverlight,Xaml,我有这样一个用户控件: public partial class MyControl : UserControl { private static readonly DependencyProperty pageContentProperty = DependencyProperty.Register("PageContent", typeof(UIElement), typeof(ActionPage), new PropertyMetadata(null)); public

我有这样一个用户控件:

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
}
现在,如果我在XAML中使用它,我必须:

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

但我只想做:

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


目前,如果我这样做,它会用
TextBlock
(这对于普通的
UserControl
)替换控件的整个内容,但我如何重写此行为?

您可以尝试将
ContentProperty
属性添加到
页面内容
属性:-

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

ContentProperty不能应用于属性,但需要应用于类。因此,我将属性移动到类定义中,作为
[ContentProperty(“PageContent”)]public particial类MyControl
,它成功了。谢谢你让我走上正轨。我将您的答案标记为答案,但可能只是修改它以备将来参考。再次感谢。嗨,安托尼琼斯!当ContentProperty是集合时,如何在xaml中直接设置。