为什么我不能将 TreeNode[] 分配给两个 Treeview [英] Why can't I assign TreeNode[] to two Treeview

查看:23
本文介绍了为什么我不能将 TreeNode[] 分配给两个 Treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这行不通,但有人可以向我解释为什么我不能这样做吗?阻止我做有问题的事情的机制是什么?

I get that this is not working, but could anybody explain to me WHY I can't do this? What's the mechanism that prevents me from doing thing in question?

所以

TreeNode[] itemNodes = new TreeNode[2];
itemNodes[0] = new TreeNode("item1");
itemNodes[1] = new TreeNode("item2");

TreeNode[] botNodesFirst = new TreeNode[2];
botNodesFirst[0] = new TreeNode("bot1");
botNodesFirst[1] = new TreeNode("bot2");

TreeNode[] botNodesSecond = new TreeNode[3];
botNodesSecond[0] = new TreeNode("bot1");
botNodesSecond[1] = new TreeNode("bot2", itemNodes);
botNodesSecond[2] = new TreeNode("bot3");

TreeNode[] topNodes = new TreeNode[2];
topNodes[0] = new TreeNode("top", botNodesFirst);
topNodes[1] = new TreeNode("top2", botNodesSecond);

然后就可以了

foreach(TreeNode node in topNodes)
{
    trvTest.Nodes.Add(node);
}

但这没有

foreach(TreeNode node in topNodes)
{
    trvTest.Nodes.Add(node);
    trvTestSecond.Nodes.Add(node); 
}

谢谢.

推荐答案

同一个 TreeNode 不能同时赋值给两个 TreeView,记住你是给一个对象赋值.在您的情况下,节点无法定义哪个控件是其父控件.

The same TreeNode cannot be assigned to two TreeView simultaneously, remember that you are assigning reference to an object. In your case node can't define which control is its parent.

但是有一件奇怪的事情,一件并不明显.不明显的是,将节点分配给新的 TreeView 是被禁止的,而它可以被重写,只是从第一棵树中消失并出现在第二棵树中.但这是设计使然,我们必须接受它,这还不错.

But there is one strange thing and one not obvious. Not obvious is that assigning node to new TreeView is prohibited while it could be rewritten and just disappear from first Tree and appear in second. But this is by design and we have to live with that, that is not so bad.

奇怪的是,我的示例项目已正确运行并在第一棵树中显示 top 和 top2 节点,在第二棵树中显示 item1 和 item2.似乎在创建视图时没有解决对树的这些分配,因为当我调试它并查看 TreeNodeCollection 内部时,继续调试引发了一个异常,即节点不能分配给多个树,您必须克隆它们或从中删除第一棵树.这是一种错误或种族.

Strange thing is that my sample project has run correctly and displayed top and top2 nodes in first tree and item1 and item2 in second tree. It seems that those assignments to trees are not resolved while creating view because when I debugged that and took a look inside TreeNodeCollection, continuation of debugging threw an exception that nodes cannot be assigned to more than one tree and you have to clone them or remove from first tree. This is kind of bug or race.

这篇关于为什么我不能将 TreeNode[] 分配给两个 Treeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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