silverlight 4带有多个内容演示者的模板控件

silverlight 4带有多个内容演示者的模板控件,silverlight,Silverlight,我正在尝试创建一个模板控件,该控件包含页眉、正文和页脚内容演示者 所以我创建了一个名为BaseDockedSectionControl的类: public class BaseDockedSectionControl:Control { public BaseDockedSectionControl() { DefaultStyleKey = typeof(BaseDockedSectionControl); } public Grid Head

我正在尝试创建一个模板控件,该控件包含页眉、正文和页脚内容演示者

所以我创建了一个名为BaseDockedSectionControl的类:

public class BaseDockedSectionControl:Control
{
    public BaseDockedSectionControl()
    {
        DefaultStyleKey = typeof(BaseDockedSectionControl);
    }

    public Grid Header
    {
        get { return (Grid)GetValue(HeaderProperty); }
        set { SetValue(HeaderProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Header.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.Register("Header", typeof(Grid), typeof(BaseDockedSectionControl), new PropertyMetadata(null));        

    public Grid Body
    {
        get { return (Grid)GetValue(BodyProperty); }
        set { SetValue(BodyProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Body.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BodyProperty =
        DependencyProperty.Register("Body", typeof(Grid), typeof(BaseDockedSectionControl), new PropertyMetadata(null));



    public Grid Footer
    {
        get { return (Grid)GetValue(FooterProperty); }
        set { SetValue(FooterProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Footer.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FooterProperty =
        DependencyProperty.Register("Footer", typeof(Grid), typeof(BaseDockedSectionControl), new PropertyMetadata(null));

          }
在App.XAML中,我向应用程序添加了一个样式。参考资料:

<Style TargetType="local:BaseDockedSectionControl">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:BaseDockedSectionControl">
                        <Grid x:Name="RootElement">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="50"/>
                                <RowDefinition />
                                <RowDefinition Height="30"/>
                            </Grid.RowDefinitions>

                            <ContentPresenter Content="{TemplateBinding Header}"/>
                            <ContentPresenter Content="{TemplateBinding Body}"    Grid.Row="1"/>
                            <ContentPresenter Content="{TemplateBinding Footer}"  Grid.Row="2"/>                            
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

然后,我创建一个名为Test.xaml的用户控件,并输入以下xaml:

<UserControl x:Class="SilverlightIdeas.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SilverlightIdeas"
             xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <local:BaseDockedSectionControl>            
            <local:BaseDockedSectionControl.Header>
                <TextBlock Text="This is the header" />
            </local:BaseDockedSectionControl.Header>
        </local:BaseDockedSectionControl>

    </Grid>
</UserControl>

当我键入TextBlock时,intellisense无法识别它,当我键入它时,我会收到消息“Property Header不支持'TextBlock'类型的值”


我做错了什么?

您在
BasedDockedSectionControl
中定义了
标题
属性,将其类型定义为
网格
文本块
不是
网格
。将
标题
属性的类型改为
控件

两件事解决了这个问题

  • 我创建了object类型的每个依赖属性

  • 事实证明,问题在于ContentPresenter似乎需要一个结束标记,而不是自结束标记:

  • 变成

    <ContentPresenter Content="{TemplateBinding Header}"></ContentPresenter>
    
    
    
    一旦我这么做了,事情就开始正常运转了