【问题标题】:silverlight 4 templated control with multiple content presenters具有多个内容演示者的 silverlight 4 模板化控件
【发布时间】:2012-11-02 00:06:49
【问题描述】:

我正在尝试创建一个具有页眉、正文和页脚内容呈现器的模板化控件。

所以我创建了一个名为 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 does not support values of type 'TextBlock'

我做错了什么?

【问题讨论】:

    标签: silverlight


    【解决方案1】:

    两件事解决了这个问题。

    1. 我做了object类型的每一个依赖属性

    2. 原来问题在于 ContentPresenter 似乎需要一个结束标记,而不是自结束标记:

    变成

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

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

    【讨论】:

      【解决方案2】:

      您已将BaseDockedSectionControl 中的Header 属性定义为Grid 类型。 TextBlock 不是 Grid。将Header 属性的类型改为Control

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-29
        • 1970-01-01
        • 2015-05-01
        相关资源
        最近更新 更多