2

我试图创建一个具有页眉、正文和页脚内容演示者的模板化控件。

所以我创建了一个名为 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 中,我向 Application.Resources 添加了一个样式:

<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’类型的值

我究竟做错了什么?

4

2 回答 2

1

有两件事解决了这个问题。

  1. 我制作了类型对象的每个依赖属性

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

变成

<ContentPresenter Content="{TemplateBinding Header}"></ContentPresenter>

一旦我这样做了,事情就开始正常工作了

于 2012-11-01T16:06:49.357 回答
0

您已将您的Header属性定义BaseDockedSectionControl为 type GridTextBlock不是一个GridHeader将属性的类型更改为Control

于 2012-10-29T01:24:05.940 回答