问题描述
大家好
可以说我有一个树视图,其中的父节点具有子节点,而子节点具有孙节点.
我要说的问题是,我选择了一个大子节点. . . .如何检测选定子代的子代节点值是什么,子代和父代值是什么?
Hi Guys
Lets say I have a treeview with parent nodes that have child nodes that have grandchild nodes.
The Question I have is lets say I select a grand child node. . . .How will I detect what that grand child nodes value is and the child and parent value is of the selected grandchild?
Thanks?
推荐答案
TreeNode parent = myNode.Parent;
TreeNode grandParent = parent == null ? null : parent.Parent;
您在谈论什么样的价值?
让我们说我有一个名为school的节点,然后该节点有教室的子节点,而教室有书桌的子节点.
换句话说:学校:教室1:桌子1
:桌子2
:教室2:桌子1
:桌子2
:教室3:桌子1
:桌子2
所以可以说我选择教室3的桌子2
我想要桌子2教室3和学校的节点值
"谢谢"
我遇到的问题是TreeNode没有值-它具有文本字段,这将为您提供来自School:class room 1:desk 2
和School:class room 2:desk 2
的"desk 2".但是,您想要的听起来像是拥有一个Desk对象,该对象与每个Desk节点相关联,并且您希望通过树进行检索.如果是这样,那很容易:每个TreeNode都有一个Tag属性,其中包含一个对象.如果将Desk实例分配给适当的TreeNode,则可以检索它:
What kind of value are you talking about?
"Lets say i have a node named school and then that node has child nodes of class rooms and the classrooms has child nodes of desks.
In other words : School : class room 1 : desk 1
: desk 2
: class room 2 : desk 1
: desk 2
: class room 3 : desk 1
: desk 2
so lets say i select desk 2 of class room 3
I want to have the node values of desk 2 class room 3 and school
''Thanks"
The problem I have is that a TreeNode doesn''t have a Value - it has a Text field, which would give you "desk 2" from both School:class room 1:desk 2
and School:class room 2:desk 2
. But what you want sound like you have a Desk object which is associated with each of the desk nodes and which you want to retrieve via the tree. If so, then that is easy: each TreeNode has a Tag property which contains an object. If you assign your Desk instance to the appropriate TreeNode, you can retrieve it:
Desk desk = myNode.Tag as Desk;
if (desk != null)
{
...
}
private string SelectNode()
{
return SelectNode(tvFileMaps.Nodes);
}
private string SelectNode(TreeNodeCollection tColl)
{
foreach (TreeNode tn in tColl)
{
if (tn.IsSelected)
{
return tn.Text;
}
else
{
return SelectNode(tn.Nodes);
}
}
return string.Empty;
}
这将返回您选择的节点值,如果您想返回父节点值,请在其中进行一些更改
This will return you the selected node value, if you want to return the parent node value then do some change in that
这篇关于TreeView节点检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!