HierarchycalDataTemplate中的WPF绑定父属性

HierarchycalDataTemplate中的WPF绑定父属性,wpf,binding,properties,parent,hierarchicaldatatemplate,Wpf,Binding,Properties,Parent,Hierarchicaldatatemplate,我有一个WPFTreeView,其中包含两个级别的数据和两个hierarchycaldatatemplate来格式化每个级别。在第二级的HierarchycalDataTemplate中,我需要在第一级的类中绑定一个属性。我试过这种方法,但不起作用: Text="{Binding Path=Ori, RelativeSource={RelativeSource TemplatedParent}}" 以Ori作为属性名称 即使这样,它也不起作用: Text="{Binding Path=tOri

我有一个WPF
TreeView
,其中包含两个级别的数据和两个
hierarchycaldatatemplate
来格式化每个级别。在第二级的
HierarchycalDataTemplate
中,我需要在第一级的类中绑定一个属性。我试过这种方法,但不起作用:

Text="{Binding Path=Ori, RelativeSource={RelativeSource TemplatedParent}}"
Ori
作为属性名称

即使这样,它也不起作用:

Text="{Binding Path=tOri, RelativeSource={RelativeSource TemplatedParent}}"
Ori
作为绑定
Ori
属性的第一个
hierarchycaldatatemplate
中的
TextBlock
的名称


您能帮助我吗?

TemplatedParent不仅指ControlTemplate中的父控件,因此不适用于DataTemplates。您可以改为使用FinDanceStore来定位父树项,然后访问其DataContext

Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}, AncestorLevel=2}, Path=DataContext.Ori}"

您误解了WPF中的
TemplatedParent
绑定
TemplatedParent
引用要扩展的继承控件。示例:如果我编写了一个
ControlTemplate
,目标是
按钮

<ControlTemplate TargetType="{x:Type Button}" x:Key="MyButtonTemplate">
   <Border BorderBrush="{TemplateBinding Property=Background}" BorderThickness="3" >
      <ContentPresenter Margin="10"/>
   </Border>
</ControlTemplate>

这将
BorderBrush
绑定到基本
按钮。Background
属性

要实现您想要的,您需要使用
relativesourcefindancestor
遍历可视化树以找到父级,然后执行绑定。帮助尝试使用或


注意:上面提供的Snoop副本存在一些严重问题,即深度不能超过256级。我有一个补丁和功能扩展版本,这是可怕的。在开发过程中使用Mole和Snoop2进行调试/可视化之间的交换

谢谢大家!!我尝试了你的解决方案,但没有成功,因为我不知道我必须编写“AncestorLevel=2”。Snoop的最新版本位于,并且除了32位和64位支持以及.NET 4之外,还有其他版本的所有组合修复。小心混淆TemplateBinding和RelativeSourceTemplatedParent。它们是做类似事情的独立机制,但TemplateBinding限制性更大,因为它只能指向父级上匹配类型的属性,并且在绑定上没有任何其他可用选项(转换器、复杂路径等)。@John:感谢Snoop上的更新;我将获取最新的并集成到我的分支中-也许我可以提供一些修复/功能。@John:是的,我应该解释一下两者之间的区别<正如您所解释的,code>TemplateBinding的局限性要大得多,它是在编译时解析的,而
relativesourcetemplatedparent
是在运行时解析的(使用绑定引擎)。