5

我有一个DataTemplate我正在使用的CellTemplatefor a GridViewColumn

我想为DataTemplate

<DataTemplate
    x:Key="_myTemplate">
    <TextBlock
        Text="{Binding Path={Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewColumn}}, Path=Header}}" />
</DataTemplate>

MyGridView绑定到 a DataTable,并且我想绑定到DataTable其名称等于附加到的 HeaderGridViewColumn的列。DataTemplate[我希望这是有道理的!]

不幸的是,这不起作用。我收到 XamlParseException:“无法在 'Binding' 类型的 'Path' 属性上设置 'Binding'。只能在 DependenceyObject 的 DependencyProperty 上设置'Binding'。”

我该如何设置?

编辑(提升 DanM 对问题的评论)

我基本上需要一个DataTemplate其绑定由和附加到DataContext哪个列。DataTemplate有替代方案吗?

4

2 回答 2

0

您不能将绑定分配给任何属性。该属性必须是该类型Binding或实现为对象的依赖属性。

Binding 类的Path属性是 typePropertyPath并且 Binding 没有将 Path 属性实现为依赖属性。因此,您不能以您尝试的方式动态绑定路径。

编辑

您基本上希望将元数据嵌入到驱动 DataTemplate 配置的绑定数据中。这不能仅在 XAML 中完成。您至少需要代码的一些支持。

在我看来,最好的方法是使用 ViewModel。这使得 XAML 中的绑定变得直截了当,并将“绑定什么与什么”决策向下推送到 ViewModel 的代码中。

于 2009-07-21T07:36:41.123 回答
0

你不就是想要这个吗?

{Binding Path=Header, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewColumn}}}
于 2009-07-22T00:30:35.070 回答