如何在自定义控件(Silverlight)中的数据模板内部使用模板绑定 [英] How to use template binding inside data template in custom control (Silverlight)

查看:132
本文介绍了如何在自定义控件(Silverlight)中的数据模板内部使用模板绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建控件,该控件将使用 ItemsSource InnerTemplate 并显示所有包裹在 CheckBox es。

I am trying to create control which will take ItemsSource and InnerTemplate and will show all the items wrapped in CheckBoxes.

该控件具有2个依赖项属性:

The control has 2 dependency properties:

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>

但是,此方法不起作用。

<$ c中的绑定$ c> ControlPresenter.ContentTemplate 使用 TemplateBinding 不起作用。

但是,当我不使用模板绑定和将该模板作为静态资源引用,然后它将按预期工作。

However, this approach does not work.
Binding in the ControlPresenter.ContentTemplate using TemplateBinding does not work.
However, when I don't use template binding and reference the template as static resource, then it works as expected.


  • 为什么我不能在datatemplate的内容演示器中使用模板绑定?

  • 我在这里想念什么?需要任何特殊的标记吗?

  • 有没有一种方法可以实现预期的行为?

请先感谢。

推荐答案

TemplateBinding只能在ControlTemplate中使用,而在DataTemplate中使用。 (DataTemplate位于ControlTemplate中这一事实无关紧要)

TemplateBinding can only be used within a ControlTemplate, you're using it within a DataTemplate. (The fact that the DataTemplate is within a ControlTemplate doesn't matter)

这篇关于如何在自定义控件(Silverlight)中的数据模板内部使用模板绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆