相关疑难解决方法(0)

如何在自定义控件(Silverlight)中的数据模板中使用模板绑定

我想创建控制,将采取ItemsSourceInnerTemplate并会显示包裹在所有项目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);
Run Code Online (Sandbox Code Playgroud)

这是模板:

<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>
Run Code Online (Sandbox Code Playgroud)

但是,这种方法不起作用.
ControlPresenter.ContentTemplate使用TemplateBinding中绑定不起作用.
但是,当我不使用模板绑定并将模板作为静态资源引用时,它会按预期工作.

  • 为什么我不能在datatemplate中的内容展示器中使用模板绑定?
  • 我在这里错过了什么?需要什么特殊标记?
  • 有没有办法实现预期的行为?

提前致谢.

silverlight datatemplate templatebinding

13
推荐指数
2
解决办法
1万
查看次数