我正在尝试创建一个控件,该控件将使用ItemsSource
和InnerTemplate
,并将显示所有包含在CheckBox
中的项目。
该控件有2个依赖属性:
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(CheckBoxWrapperList), null);
public static readonly DependencyProperty InnerTemplateProperty = DependencyProperty.Register("InnerTemplate", typeof(DataTemplate), typeof(CheckBoxWrapperList), null);
这是模板:
<ControlTemplate TargetType="local:CheckBoxWrapperList">
<Grid>
<Grid.Resources>
<DataTemplate x:Key="wrapper">
<CheckBox>
<ContentPresenter ContentTemplate="{TemplateBinding InnerTemplate}" Content="{Binding}" />
</CheckBox>
</DataTemplate>
</Grid.Resources>
<ItemsControl ItemTemplate="{StaticResource wrapper}" ItemsSource="{TemplateBinding ItemsSource}" />
</Grid>
</ControlTemplate>
然而,这种方法不起作用。使用
TemplateBinding
将绑定到ControlPresenter.ContentTemplate
中的内容模板不起作用。但是,当我不使用模板绑定并将模板作为静态资源引用时,它按预期工作。
- 为什么不能在datatemplate中的content presenter中使用模板绑定?
- 我漏了什么吗?需要任何特殊标记吗?
- 有没有办法实现预期的行为?
谢谢提前。