我试图创建一个具有页眉、正文和页脚内容演示者的模板化控件。
所以我创建了一个名为 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’类型的值
我究竟做错了什么?