ObjectListView - TreeListView expand different lists in tree structure

元气小坏坏 提交于 2020-01-24 00:44:29

问题


I try to expand multiple lists in my tree structure.

Let's say I have the following classes. I have the class Product, which contains a list with its children. This is my actual tree structure.

    class Product
{
    int prodID;
    string prodName;

    List<Product> prodChildren;
    List<Article> articleList;

    //maybe further list...

    public int ProdID
    {
        get { return prodID;}
        set{prodID = value;}
    }

    public string ProdName
    {
        get { return prodName;}
        set {prodName = value;}
    }

    public Product(int id, string name)
    {
        ProdID = id;
        ProdName = name;

        prodChildren = new List<Product>();
        articleList = new List<Article>();
    }

    //...
    //addProdChildren
    //...
    //addArticles
}

 class SalesArticle
{
    int articleNumber;
    string articleName;

    public int ArticleNumber
    {
        get
        {
            return articleNumber;
        }

        set
        {
            articleNumber = value;
        }
    }

    public string ArticleName
    {
        get
        {
            return articleName;
        }

        set
        {
            articleName = value;
        }
    }

    public SalesArticle(int no, string name)
    {
        ArticleNumber = no;
        ArticleName = name;
    }
}

This part works great so far, but how can I expand the articles list in the same tree? I can't get this part working.

Should look something like:

[product1]
---------[product2]
---------[product3]
--------------[article1]
--------------[article2]
--------------[article3]
--------------[article4]
---------[product4]
--------------[product5]
----------------------[articleX]
----------------------[articleX]
----------------------[articleX]
----------------------[articleX]

My CanExpandGetter and ChildrenGetter are very basic currently

            tlvProduktStruktur.CanExpandGetter = delegate (object x) { return (x is Product); };

        tlvProduktStruktur.ChildrenGetter = delegate (object x) 
        {
            if (((Product)x).ProduktChildren.Count > 0)
                return ((Product)x).prodChildren;
            else
                return null;

        };

Update

---------------------------------------

I found a solution if anyone is interested:

My product class has a list of objects:

    class Product
{
    int prodID;
    string prodName;

    List<object> childItems;

    ...
}

Now I can add every type of objects or other lists to this object-list, for example:

            Product rootProduct = new Product(1, "root");

        Article artikel1 = new Article();
        Article artikel2 = new Article();

        //this root product has a working process which contains a list of articles
        WorkingProcess wp = new WorkingProcess();
        wp.add(artikel1);
        wp.add(artikel2);

        childItems.Add(wp);

        //let's assume the root product has also two children
        Product p1 = new Product(2, "sub product 1");
        Product p2 = new Product(3, "sub product 2");

        rootProduct.childItems.Add(p1);
        rootProduct.childItems.Add(p2);

The structure looks like the following:

rootProduct
------WorkingProcess
-----------Article1
-----------Article2
------SubProduct1
------SubProduct2

Now we only need to change the CanExpandGetter and ChildrenGetter:

            // when the node can be expanded

tlvProduktStruktur.CanExpandGetter = delegate (object x) {

            //if the product has child items (working process and/or child products) show it as expandable
            if (x is Product)
                if (((Product)x).ChildItems.Count > 0)
                    return true;

            //if the working process has articles, show it as expandable
            if (x is WorkingProcess)
                if (((WorkingProcess)x).ArticleList.Count > 0)
                    return true;

            return false;

        };


        tlvProduktStruktur.ChildrenGetter = delegate (object x) {

//check the type and expand its children

            //show the child items (working process and/or child products) of the current product
            if (x is Product)
                if (((Product)x).ChildItems.Count > 0)
                {
                    return ((Product)x).ChildItems;
                }

            //show the articles of the working process
            if (x is WorkingProcess)
                if (((WorkingProcess)x).ArticleList.Count > 0)
                    return ((WorkingProcess)x).ArticleList;

            return new List<object>();

        };

Now you should get the expected result.

来源:https://stackoverflow.com/questions/37742624/objectlistview-treelistview-expand-different-lists-in-tree-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!