如何创建具有两个内容区域的Silverlight控件

如何创建具有两个内容区域的Silverlight控件,silverlight,Silverlight,我想创建一个Silverlight 2控件,它有两个内容区域。标题和主要内容。因此,控制将是: <StackPanel> <TextBlock Text=" CONTENT1 "/> <Content with CONTENT2 "/> </StackPanel> 当我使用控件时,我应该能够使用: <MyControl Text="somecontent">main content </MyControl> 如何创建这样

我想创建一个Silverlight 2控件,它有两个内容区域。标题和主要内容。因此,控制将是:

<StackPanel>
<TextBlock Text=" CONTENT1 "/>
<Content with CONTENT2 "/>
</StackPanel>
当我使用控件时,我应该能够使用:

<MyControl Text="somecontent">main content </MyControl>

如何创建这样的控件?

您需要的是WPF HeaderedContentControl的Silverlight版本
你可以在这里试试

使用该属性可以很容易地做到这一点

然后,您可以将代码定义为:

[ContentProperty("Child")]
public partial class MyControl: UserControl
{
    public static readonly DependencyProperty ChildProperty = DependencyProperty.Register("Child", typeof(UIElement), typeof(MyControl), null);

    public UIElement Child
    {
        get { return (UIElement)this.GetValue(ChildProperty); }
        set
        {
            this.SetValue(ChildProperty, value);
            this.content.Content = value;
        }
    }
这样做的目的是将标记主内容中的任何默认内容设置为类的子属性。一旦设置好,你就可以把它分配给任何你喜欢的控件

编辑:

您可以拥有任意数量的内容,但只能拥有1个通过ContentProperty属性指定的自动内容。 如果您想要两个,您可以:

<MyControl>
  <MyControl.Content1>Hello World</MyControl.Content1>
  <MyControl.Content2>Goodbye World</MyControl.Content2>
</MyControl>

你所要做的就是确保你的代码中有匹配的依赖属性。设置属性后,只需将其分配给XAML中的父内容控件。

Perfect,我可以有两个内容吗?在我的问题中回答,因为这里没有足够的字符。