HierarchicalDataTemplate 中的 WPF 绑定父属性 [英] WPF Binding parent property in HierarchicalDataTemplate

查看:44
本文介绍了HierarchicalDataTemplate 中的 WPF 绑定父属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF TreeView 有 2 个数据级别和 2 个 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}}"

Ori为属性名称

即使这样也行不通:

Text="{Binding Path=tOri, RelativeSource={RelativeSource TemplatedParent}}"

使用 tOri 作为绑定 Ori 属性的第一个 HierarchicalDataTemplateTextBlock 的名称.

with tOri as the name of the TextBlock in the fisrt HierarchicalDataTemplate that bind the Ori propery.

你能帮我吗?

推荐答案

TemplatedParent 仅指 ControlTemplate 内的父控件,因此不适用于 DataTemplates.您可以改用 FindAncestor 来定位父 TreeViewItem,然后访问其 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}"

这篇关于HierarchicalDataTemplate 中的 WPF 绑定父属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆