【发布时间】:2011-06-05 20:00:09
【问题描述】:
我正在尝试创建将采用ItemsSource 和InnerTemplate 的控件,并将显示包含在CheckBoxes 中的所有项目。
该控件有 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 的内容展示器中使用模板绑定?
- 我在这里缺少什么?需要什么特殊标记吗?
- 有没有办法实现预期的行为?
提前致谢。
【问题讨论】:
-
你明白了吗?我也有同样的问题。
标签: silverlight datatemplate templatebinding