我正在尝试创建一个具有页眉,正文和页脚内容演示者的模板化控件。
所以我创建了一个名为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'的值
我做错了什么?
答案 0 :(得分:1)
解决了两个问题。
我创建了object
原来问题是ContentPresenter似乎需要有一个结束标记,而不是自结束标记:
变为
<ContentPresenter Content="{TemplateBinding Header}"></ContentPresenter>
一旦我这样做,事情就开始正常工作了
答案 1 :(得分:0)
您已将Header
中的BaseDockedSectionControl
媒体资源定义为Grid
类型。 TextBlock
不是Grid
。请改为将Header
属性的类型更改为Control
。