2012-10-26 50 views
2

我試圖創建一個具有標題,正文和頁腳內容演示者的模板控件。silverlight 4具有多個內容演示者的模板控件

因此,我創建了一個名爲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> 

我然後創建一個名爲測試的用戶控件。 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的輸入,智能感知母鹿snt識別它,當我輸入時,我收到消息「屬性標題不支持類型'TextBlock'的值

我在做什麼錯?

回答

1

有兩件事解決了這個問題。

  1. 我做了每種類型的對象的依賴屬性

  2. 原來,問題是,ContentPresenter似乎需要有一個結束標籤,而不是自結束標籤:

變得

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

一旦我做到了,事情就開始正常工作

0

您已將Header屬性BaseDockedSectionControl定義爲Grid類型。 TextBlock不是Grid。改爲將Header屬性的類型更改爲Control

相關問題