DataTemplate 中的 TemplateBinding 不起作用

TemplateBinding inside DataTemplate not working

我正在我的通用 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>

您不能在 DataTemplate 中使用 TemplateBinding,但有解决方法:

  1. 您可以使用 DataContext 为您需要的某些隐藏元素创建某种代理。更多详情 here.

  2. 您可以创建某种 RelativeSource 绑定来查找 Ancestor 就像 WPF. More details here