【发布时间】:2010-11-12 13:03:15
【问题描述】:
我有一个DataTemplate,我将其用作CellTemplate 的GridViewColumn。
我想为DataTemplate写这样的东西:
<DataTemplate
x:Key="_myTemplate">
<TextBlock
Text="{Binding Path={Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewColumn}}, Path=Header}}" />
</DataTemplate>
我的GridView 绑定到DataTable,我想绑定到DataTable 的列,其名称等于GridViewColumn 的标题,DataTemplate 附加到。 [我希望这是有道理的!]
很遗憾,这不起作用。我收到 XamlParseException:“无法在 'Binding' 类型的 'Path' 属性上设置 'Binding'。只能在 DependenceyObject 的 DependencyProperty 上设置'Binding'。”
我该如何设置?
编辑(提升 DanM 对问题的评论)
我基本上需要一个DataTemplate,其绑定由DataContext 以及DataTemplate 附加到哪个列确定。有其他选择吗?
【问题讨论】:
-
不确定为什么您将其标记为 silverlight 不支持 Silverlight RelativeSource
-
抱歉,安东尼。我不知道silverlight 不支持RelativeSource。不过,我不喜欢使用 RelativeSource,所以如果我的问题有一个 XAML 解决方案不使用它,我仍然有兴趣听到它。
标签: wpf xaml binding datatemplate