在WPF中动态地进行Treeview [英] Treeview Dynamically in WPF

查看:221
本文介绍了在WPF中动态地进行Treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助,这是我的代码,可以正常工作.当我像这样更改(mylist)顺序时,会给出错误的树视图:

Please help, here is my code it''s work proper. When I change the (mylist) order just like this then it give wrong treeview:

myList.Add(new MyMenuItem(1, "Country", 0));
myList.Add(new MyMenuItem(8, "U.S.A.", 1));
myList.Add(new MyMenuItem(3, "India", 0));
myList.Add(new MyMenuItem(10, "mj", 9));
myList.Add(new MyMenuItem(4, "AP", 11));
myList.Add(new MyMenuItem(5, "UP", 3));
myList.Add(new MyMenuItem(6, "CA", 2));
myList.Add(new MyMenuItem(7, "NY", 2));
myList.Add(new MyMenuItem(2, "jj", 5));
myList.Add(new MyMenuItem(9, "mj", 4));
myList.Add(new MyMenuItem(11, "kk", 10));
myList.Add(new MyMenuItem(12, "mj", 11));
myList.Add(new MyMenuItem(13, "India", 0));


<grid>
    <TreeView name="myTreeView" />
</grid>

在代码中:

public partial class Window11 : Window
{
  public Window11()
  {
    InitializeComponent();
    //I created my objects here, since I have no DataTable
    //It is assumed the the Parents appear before any child
    List<mymenuitem> myList = new List<mymenuitem>();
    myList.Add(new MyMenuItem(1, "Country", 0));
    myList.Add(new MyMenuItem(2, "U.S.A.", 1));
    myList.Add(new MyMenuItem(3, "India", 1));
    myList.Add(new MyMenuItem(4, "AP", 3));
    myList.Add(new MyMenuItem(5, "UP", 3));
    myList.Add(new MyMenuItem(6, "CA", 2));
    myList.Add(new MyMenuItem(7, "NY", 2));
    Dictionary<int,> flattenedTree = new Dictionary<int,>();
    foreach (MyMenuItem item in myList)
    {
      TreeViewItem treeNode = new TreeViewItem();
      treeNode.Header = item.Header;
      treeNode.Tag = item;
      flattenedTree.Add(item.ID, treeNode);
      if (flattenedTree.ContainsKey(item.ParentID))
      {
        flattenedTree[item.ParentID].Items.Add(treeNode);
      }
      else
      {
        myTreeView.Items.Add(treeNode);
      }
    }
  }
}

  class MyMenuItem
  {
    internal int ID {private set;get;}
    internal string Header { private set; get; }
    internal int ParentID { private set; get; }
    internal MyMenuItem(int id, string header, int parentId)
    {
      ID = id;
      Header = header;
      ParentID = parentId;
    }
  }

推荐答案

如果您像在代码中那样对数据进行硬编码,那么您将永远走不远.与之相关的一个问题是:您残酷地违反了第一项原则:不要重复自己,请参见 ^ ].如果您仍然不明白什么是坏处,请仔细阅读本文.

您需要将国家/地区和其他数据保留在代码之外的数据中.它不必是外部文件,也可以是可执行文件中嵌入的资源.无论您做什么,它都应该是一个单一的数据源.您可以读取此数据并填充不同的控件,列表,树或菜单-对其进行命名,在计算中使用等.

—SA
You will never go far if you keep hard-coding data in your code like you do. A problem related to it is: you brutally violate one of the first principles: Don''t Repeat Yourself, see http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^]. If you still don''t understand what is bad, read this article thoroughly.

You need to keep your countries and other data outside of your code, in data. It does not have to be an external file, it could be a resource embedded in your executable. Whatever you do, it should be one single source of data. You can read this data and populate different controls, lists or trees or menus — you name it, use in your calculations, etc.

—SA


这篇关于在WPF中动态地进行Treeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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