TreeNodeCollection.ContainsKey() 的问题
我正在为一个适用于家庭概念的应用程序创建一个插件。 每个系列都属于一个系列类别,并且每个系列都包含系列符号。 像这样的漂亮树结构:
- FamilyCategory(门)
- 家庭(外门)
- 家庭符号(门 2000x1000)
- 家庭符号(门 2000x900)
- 家庭(车库门)
- 家庭符号(门 2000x2000)
- 家庭符号(门 2100x2000)
- 家庭(外门)
- FamilyCategory (Windows)
- 家庭(单窗)
- FamilySymbol(窗口 1000x1400)
- FamilySymbol(窗口 800x1400)
- 家庭(双窗)
- FamilySymbol(窗口 2000x1400)
- FamilySymbol(窗口 2100x1400)
- 家庭(单窗)
现在我正在尝试构建一个代表该结构的 TreeView。我有一个 Family 对象列表,每个对象都有一个 FamilyCategory 属性。我试图确定具有 FamilyCategory 名称的 TreeNode 是否已存在,如果存在,我尝试将 Family 添加到该节点。如果该类别的节点不存在,我会创建一个新节点并在其中添加族。 不幸的是,下面的代码总是将categoryExists 评估为 false。
foreach (Family family in families)
{
string familyCategoryName = family.FamilyCategory.Name;
bool categoryExists = treeView.Nodes.ContainsKey(familyCategoryName);
if (categoryExists)
{
categoryNode = treeView.Nodes[familyCategoryName];
}
else
{
categoryNode = new TreeNode(familyCategoryName);
treeView.Nodes.Add(categoryNode);
}
TreeNode familyNode = new TreeNode(family.Name);
categoryNode.Nodes.Add(familyNode);
foreach (FamilySymbol familySymbol in family.Symbols)
{
familyNode.Nodes.Add(familySymbol.Name);
}
}
我做错了什么?
I'm creating a plugin for an application which works with a concept of families.
Each Family belongs to a FamilyCategory and each Family contains FamilySymbols.
A nice tree structure like that:
- FamilyCategory (Doors)
- Family (External Doors)
- FamilySymbol (Door 2000x1000)
- FamilySymbol (Door 2000x900)
- Family (Garage Doors)
- FamilySymbol (Door 2000x2000)
- FamilySymbol (Door 2100x2000)
- Family (External Doors)
- FamilyCategory (Windows)
- Family (Single Windows)
- FamilySymbol (Window 1000x1400)
- FamilySymbol (Window 800x1400)
- Family (Double Windows)
- FamilySymbol (Window 2000x1400)
- FamilySymbol (Window 2100x1400)
- Family (Single Windows)
Now I'm trying to build a TreeView representing that structure. I have a list of Family objects and each of them has a FamilyCategory property. I'm trying to determine if a TreeNode with FamilyCategory name already exists and if it does I'm trying to add the Family to that node. If a node for that category doesn't exist I create a new one and add the family there.
Unfortunately the code below always evaluates categoryExists as false.
foreach (Family family in families)
{
string familyCategoryName = family.FamilyCategory.Name;
bool categoryExists = treeView.Nodes.ContainsKey(familyCategoryName);
if (categoryExists)
{
categoryNode = treeView.Nodes[familyCategoryName];
}
else
{
categoryNode = new TreeNode(familyCategoryName);
treeView.Nodes.Add(categoryNode);
}
TreeNode familyNode = new TreeNode(family.Name);
categoryNode.Nodes.Add(familyNode);
foreach (FamilySymbol familySymbol in family.Symbols)
{
familyNode.Nodes.Add(familySymbol.Name);
}
}
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将其替换
为:(
TreeNodeCollection.ContainsKey()
搜索Name
属性,而不是Text
属性)try replacing this:
by this:
(
TreeNodeCollection.ContainsKey()
searches theName
Property, not theText
Property)