TreeNodeCollection.ContainsKey() 的问题

发布于 2024-11-08 15:08:45 字数 1390 浏览 0 评论 0原文

我正在为一个适用于家庭概念的应用程序创建一个插件。 每个系列都属于一个系列类别,并且每个系列都包含系列符号。 像这样的漂亮树结构:

  • 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)
  • FamilyCategory (Windows)
    • Family (Single Windows)
      • FamilySymbol (Window 1000x1400)
      • FamilySymbol (Window 800x1400)
    • Family (Double Windows)
      • FamilySymbol (Window 2000x1400)
      • FamilySymbol (Window 2100x1400)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我恋#小黄人 2024-11-15 15:08:45

尝试将其替换

categoryNode = new TreeNode(familyCategoryName);
treeView.Nodes.Add(categoryNode);

为:(

categoryNode = new TreeNode(familyCategoryName);
categoryNode.Name = familyCategoryName;
treeView.Nodes.Add(categoryNode);

TreeNodeCollection.ContainsKey() 搜索 Name 属性,而不是 Text 属性)

try replacing this:

categoryNode = new TreeNode(familyCategoryName);
treeView.Nodes.Add(categoryNode);

by this:

categoryNode = new TreeNode(familyCategoryName);
categoryNode.Name = familyCategoryName;
treeView.Nodes.Add(categoryNode);

(TreeNodeCollection.ContainsKey() searches the Name Property, not the Text Property)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文