WPF绑定parent属性在HierarchicalDataTemplate [英] WPF Binding parent property in HierarchicalDataTemplate
问题描述
我有一个WPF TreeView控件
与2级数据和2 <code> HierarchicalDataTemplate 每个级别的格式。从 HierarchicalDataTemplate
在第二个层面上,我需要一个属性类中的第一级的绑定。我曾尝试在这种方式,但它不到风度工作
I have a WPF TreeView
with 2 levels of data and 2 HierarchicalDataTemplate
to format each level. From the HierarchicalDataTemplate
at the second level, I need to bind a property in the class of the first level. I have tried in this way, but it dosn't work:
Text="{Binding Path=Ori, RelativeSource={RelativeSource TemplatedParent}}"
与大利
作为属性格式的名称
即使在这种方式不到风度作品:
Even in this way it dosn't works:
Text="{Binding Path=tOri, RelativeSource={RelativeSource TemplatedParent}}"
与东篱
作为的的TextBlock
在最前一页的名称 HierarchicalDataTemplate
结合的大利
属性格式。
with tOri
as the name of the TextBlock
in the fisrt HierarchicalDataTemplate
that bind the Ori
propery.
你能帮助我吗?
推荐答案
TemplatedParent仅指一个的ControlTemplate内的父级控制,因此不会与的DataTemplates工作。您可以使用FindAncestor而是找到父树型视图,然后访问它的DataContext的。
TemplatedParent only refers to the parent Control inside a ControlTemplate and so doesn't work with DataTemplates. You can use FindAncestor instead to locate the parent TreeViewItem and then access its DataContext.
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}, AncestorLevel=2}, Path=DataContext.Ori}"
这篇关于WPF绑定parent属性在HierarchicalDataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!