里面的DataTemplate TemplateBinding不工作

问题描述 投票:4回答:1

我在我的通用Windows应用程序创建一个模板化控件。

问题是,在<Button Content="{Binding}" Command="{TemplateBinding AddCharCommand}" />TemplateBinding不起作用。

看来这个问题是因为它是一个DataTemplate中定义。

这是Style并应用到我的控制模板。

<Style TargetType="local:CoordinatesControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:CoordinatesControl">

                <ListView ItemsSource="{TemplateBinding Numbers}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Button Content="{Binding}"
                                    Command="{TemplateBinding AddCharCommand}" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
.net xaml uwp windows-10-universal
1个回答
0
投票

你不能里面TemplateBinding使用DataTemplate,但也有变通方法:

  1. 您可以创建类型的代理与DataContext你需要一些隐藏的元素。更多细节here
  2. 您可以创建一种RelativeSource结合找到AncestorWPF的。更多细节here
© www.soinside.com 2019 - 2024. All rights reserved.