4

我正在使用内置的 z80 导航控件,这里是演示链接: Z80 Navigation Menu

如果有人看到该控件,它就会有一个对象来创建类似父菜单的菜单,并在其下创建子菜单。类似于以下内容:

public List<NavBarItem> sampleDynamicNav; //List of navbar objects
public DemoItems()
{
    //Create object instance here and assign the parent as well child menus here
    sampleDynamicNav = new List<NavBarItem> {
    new NavBarItem {ID = 1, Text = "UserInfo", Icon = new ItemIcon {Default = SampleProject.Properties.Resources.nav_new_home, Hover = SampleProject.Properties.Resources.nav_new_home, Selected = SampleProject.Properties.Resources.nav_new_home}, ToolTip = "tooltip Main Menu", Height = 40,
        Icon = new ItemIcon {Default = SampleProject.Properties.Resources.nav_new_home, Hover = SampleProject.Properties.Resources.nav_new_home, Selected = SampleProject.Properties.Resources.nav_new_home }, ToolTip = "tooltip Desktop"},
        Childs = new List<NavBarItem> {
                    new NavBarItem {ID = 41, Text = "Add/Edit Users", Height = 30 },
                    new NavBarItem {ID = 42, ParentID = 1, Text = "Inactive User", Height = 30}
    };
}

如果我们静态分配菜单,这非常简单。但我坚持下去,当尝试动态添加它们时,我的意思是从数据库创建菜单,如下所示:

public DemoItems()
{
    foreach (var parent in GetParent("USER-0001"))
    {
          foreach (var child in GetChild(parent.MenuNo))
          {
            sampleDynamicNav = new List<NavBarItem> {
                 new NavBarItem {
                 ID = parent.MenuNo, Text = parent.MenuName, Icon = new ItemIcon {Default =  SampleProject.Properties.Resources.nav_new_home, Hover = SampleProject.Properties.Resources.nav_new_home, Selected = SampleProject.Properties.Resources.nav_new_home}, ToolTip = "tooltip Main Menu", Height = 40,
                 Childs = new List<NavBarItem> {
                                    new NavBarItem {ID = child.MenuNo, ParentID = parent.MenuNo, Text = child.MenuName, Height = 30 },
                           }
                     }
               };
          }
     }
}

使用上面的代码,它应该至少在导航栏中获得父菜单。现在,将子菜单放在一边,它在导航栏中显示一个父菜单,如下所示:

样品 1

但它应该如下所示,因为有两个父菜单并使用foreach循环迭代列表(GetParents() 返回对象列表):

样品 2

我不知道我是否需要为此做任何其他事情,并且想知道我是否可以遍历导航栏的子属性,如下所示:

foreach (var child in GetChild(parent.MenuNo))
{
   Childs = new List<NavBarItem> {
            new NavBarItem {ID = child.MenuNo, ParentID = parent.MenuNo, Text = child.MenuName, Height = 30 },
}

注意:当尝试使用循环迭代子属性时,它现在会抛出错误。第二个内部循环也可以工作并取出子菜单,但是说父菜单有两个子菜单,它当时返回 1。我调试了列表,它像往常一样返回两个父菜单,但没有显示在导航栏中。

GetParents 方法

/**Get Menu Details - Starts**/
public IEnumerable<UserViewModel> GetParent(string empNo)
{
       List<UserViewModel> lstUser = new List<UserViewModel>();

       string query = "SELECT DISTINCT M.PARENT, M.MENUNO, M.MENUNAME FROM (SELECT DISTINCT M.MENUNO, M.MENUNAME, M.PARENT " +
                      "FROM USER_DETAILS U INNER JOIN USER_GROUP_DETAILS UG ON UG.EMPNO = U.EMPNO " +
                      "INNER JOIN ASSIGN_MENU_DETAILS AM ON AM.GROUPNO = UG.GROUPNO INNER JOIN MENU_DETAILS M " +
                      "ON M.MENUNO = AM.MENUNO WHERE U.EMPNO = '" + empNo + "' " +
                      "UNION ALL " +
                      "SELECT DISTINCT M.MENUNO, M.MENUNAME, " +
                      "M.PARENT FROM MENU_DETAILS M " +
                      "INNER JOIN MENU_DETAILS C " +
                      "ON C.PARENT = M.MENUNO) m WHERE M.PARENT = '0' ORDER BY M.PARENT";

        DataTable dt = SelectData(query);

        if (dt != null && dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                UserViewModel bo = new UserViewModel();
                bo.Parent = Convert.ToInt32(dr["PARENT"].ToString());
                bo.MenuNo = Convert.ToInt32(dr["MENUNO"].ToString());
                bo.MenuName = dr["MENUNAME"].ToString();

                lstUser.Add(bo);
            }
        }
   return lstUser;
}
/**Get Menu Details - Ends**/
4

2 回答 2

3

您可以创建以下帮助方法并使用它们来创建一个List<NavBarItem接受任何类型的数据源作为输入,包括DataTableList<YourEntity>或任何其他是IEnumerable<T>.

因此,无论您拥有哪种数据存储,都可以使用以下方法。

它依赖于递归算法来创建树。要从任何类型的数据源创建树,您需要具备以下信息:

  1. 数据源
  2. 如何检测数据源中的项目是否为根项目
  3. 如何在数据源中查找项目的子项目
  4. 如何从数据源项创建树项。

以下方法NavBarItem通过询问上述信息来创建层次结构列表:

private IEnumerable<NavBarItem> GetNavBarItems<T>(
    IEnumerable<T> source,
    Func<T, Boolean> isRoot,
    Func<T, IEnumerable<T>, IEnumerable<T>> getChilds,
    Func<T, NavBarItem> getItem)
{
    IEnumerable<T> roots = source.Where(x => isRoot(x));
    foreach (T root in roots)
        yield return ConvertEntityToNavBarItem(root, source, getChilds, getItem); ;
}

private NavBarItem ConvertEntityToNavBarItem<T>(
    T entity,
    IEnumerable<T> source,
    Func<T, IEnumerable<T>, IEnumerable<T>> getChilds,
    Func<T, NavBarItem> getItem)
{
    NavBarItem node = getItem(entity);
    var childs = getChilds(entity, source);
    foreach (T child in childs)
        node.Childs.Add(ConvertEntityToNavBarItem(child, source, getChilds, getItem));
    return node;
}

例子

我假设您已将数据加载到以下结构中:

var dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("ParentId", typeof(int));

dt.Rows.Add(1, "Menu 1", DBNull.Value);
dt.Rows.Add(11, "Menu 1-1", 1);
dt.Rows.Add(111, "Menu 1-1-1", 11);
dt.Rows.Add(112, "Menu 1-1-2", 11);
dt.Rows.Add(12, "Menu 1-2", 1);
dt.Rows.Add(121, "Menu 1-2-1", 12);
dt.Rows.Add(122, "Menu 1-2-2", 12);
dt.Rows.Add(123, "Menu 1-2-3", 12);
dt.Rows.Add(124, "Menu 1-2-4", 12);
dt.Rows.Add(2, "Menu 2", DBNull.Value);
dt.Rows.Add(21, "Menu 2-1", 2);
dt.Rows.Add(211, "Menu 2-1-1", 21);

然后要将其转换为 a List<NavBarItem>,您可以使用以下代码:

var source = dt.AsEnumerable();
var list = GetNavBarItems(
        source,
        (r) => r.Field<int?>("ParentId") == null,
        (r, s) => s.Where(x => x.Field<int?>("ParentId") == r.Field<int?>("Id")),
        (r) => new NavBarItem()
        {
            ID = r.Field<int>("Id"),
            Text = r.Field<string>("Name"),
            ParentID = r.Field<int?>("ParentId")
        }).ToList();

结果,您将具有以下结构:

Menu 1
   Menu 1-1
       Menu 1-1-1
       Menu 1-1-2
   Menu 1-2
       Menu 1-2-1
       Menu 1-2-2
       Menu 1-2-3
       Menu 1-2-4
Menu 2
    Menu 2-1
        Menu 2-1-1

笔记

对于那些不想安装包,但想测试结构的人,可以使用以下NavBarItem类:

public class NavBarItem
{
    public NavBarItem()
    {
        Childs = new List<NavBarItem>();
    }
    public int ID { get; set; }
    public int? ParentID { get; set; }
    public string Text { get; set; }
    public List<NavBarItem> Childs { get; set; }
    public override string ToString()
    {
        return Text;
    }
}
于 2018-12-24T10:47:56.160 回答
1

我使用您的代码示例@Reza Aghaei 尝试了以下操作,并使用TreeView如下控制卡住了一点:(它可以工作,但下面一行出现错误)

private void frmSampleApp_Load(object sender, EventArgs e)
{
    var dt = new DataTable();
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("ParentId", typeof(int));

    dt.Rows.Add(1, "Menu 1", DBNull.Value);
    dt.Rows.Add(11, "Menu 1-1", 1);
    dt.Rows.Add(111, "Menu 1-1-1", 11);
    dt.Rows.Add(112, "Menu 1-1-2", 11);
    dt.Rows.Add(12, "Menu 1-2", 1);
    dt.Rows.Add(121, "Menu 1-2-1", 12);
    dt.Rows.Add(122, "Menu 1-2-2", 12);
    dt.Rows.Add(123, "Menu 1-2-3", 12);
    dt.Rows.Add(124, "Menu 1-2-4", 12);
    dt.Rows.Add(2, "Menu 2", DBNull.Value);
    dt.Rows.Add(21, "Menu 2-1", 2);
    dt.Rows.Add(211, "Menu 2-1-1", 21);

    var source = dt.AsEnumerable();
    var list = GetNavBarItems(
            source,
            (r) => r.Field<int?>("ParentId") == null,
            (r, s) => s.Where(x => x.Field<int?>("ParentId") == r.Field<int?>("Id")),
            (r) => new NavBarItem()
            {
                ID = r.Field<int>("Id"),
                Text = r.Field<string>("Name"),
                ParentID = r.Field<int?>("ParentId")
            }).ToList();

    foreach (var item in list)
    {
        TreeNode parentNode = null;
        parentNode = treeView1.Nodes.Add(item.Text.ToString());

        BindData(Convert.ToInt32(item.ParentID), parentNode);
    }
}

public void BindData(int parentId, TreeNode parentNode)
{
    var dt = new DataTable();
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("ParentId", typeof(int));

    dt.Rows.Add(1, "Menu 1", DBNull.Value);
    dt.Rows.Add(11, "Menu 1-1", 1);
    dt.Rows.Add(111, "Menu 1-1-1", 11);
    dt.Rows.Add(112, "Menu 1-1-2", 11);
    dt.Rows.Add(12, "Menu 1-2", 1);
    dt.Rows.Add(121, "Menu 1-2-1", 12);
    dt.Rows.Add(122, "Menu 1-2-2", 12);
    dt.Rows.Add(123, "Menu 1-2-3", 12);
    dt.Rows.Add(124, "Menu 1-2-4", 12);
    dt.Rows.Add(2, "Menu 2", DBNull.Value);
    dt.Rows.Add(21, "Menu 2-1", 2);
    dt.Rows.Add(211, "Menu 2-1-1", 21);

    var source = dt.AsEnumerable();
    var list = GetNavBarItems(
            source,
            (r) => r.Field<int?>("ParentId") == null,
            (r, s) => s.Where(x => x.Field<int?>("ParentId") == r.Field<int?>("Id")),
            (r) => new NavBarItem()
            {
                ID = r.Field<int>("Id"),
                Text = r.Field<string>("Name"),
                ParentID = r.Field<int?>("ParentId")
            }).ToList();
        TreeNode childNode; 

    foreach (var item in list)
    {
        if (parentNode == null)

            childNode = treeView1.Nodes.Add(item.Text.ToString());

        else

            childNode = parentNode.Nodes.Add(item.Text.ToString());
        BindData(Convert.ToInt32(item.ID.ToString()), childNode); //An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll
    }
}

private IEnumerable<NavBarItem> GetNavBarItems<T>(
IEnumerable<T> source,
Func<T, Boolean> isRoot,
Func<T, IEnumerable<T>, IEnumerable<T>> getChilds,
Func<T, NavBarItem> getItem)
    {
        IEnumerable<T> roots = source.Where(x => isRoot(x));
        foreach (T root in roots)
            yield return ConvertEntityToNavBarItem(root, source, getChilds, getItem); ;
    }

    private NavBarItem ConvertEntityToNavBarItem<T>(
    T entity,
    IEnumerable<T> source,
    Func<T, IEnumerable<T>, IEnumerable<T>> getChilds,
    Func<T, NavBarItem> getItem)
    {
        NavBarItem node = getItem(entity);
        var childs = getChilds(entity, source);
        foreach (T child in childs)
            node.Childs.Add(ConvertEntityToNavBarItem(child, source, getChilds, getItem));
        return node;
    }
}

public class NavBarItem
{
    public NavBarItem()
    {
        Childs = new List<NavBarItem>();
    }
    public int ID { get; set; }
    public int? ParentID { get; set; }
    public string Text { get; set; }
    public List<NavBarItem> Childs { get; set; }
    public override string ToString()
    {
        return Text;
    }
}

注意:没关系。我知道,这里一团糟,只是想弄清楚学习的目的-谢谢。

于 2018-12-25T18:04:04.777 回答