我尝试在我的树结构中扩展多个列表。
假设我有以下课程。我有 Product 类,其中包含一个包含其子项的列表。这是我的实际树结构。
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;
}
}
到目前为止,这部分效果很好,但是如何在同一棵树中展开文章列表?我无法让这部分工作。
应该看起来像:
[product1]
---------[product2]
---------[product3]
--------------[article1]
--------------[article2]
--------------[article3]
--------------[article4]
---------[product4]
--------------[product5]
----------------------[articleX]
----------------------[articleX]
----------------------[articleX]
----------------------[articleX]
我的 CanExpandGetter 和 ChildrenGetter 目前非常基础
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;
};
更新
--------------------------------------
如果有人感兴趣,我找到了解决方案:
我的产品类有一个对象列表:
class Product
{
int prodID;
string prodName;
List<object> childItems;
...
}
现在我可以将每种类型的对象或其他列表添加到此对象列表中,例如:
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);
结构如下所示:
根产品 - - - 工作流程 - - - - - -第1条 ------------第2条 ------子产品1 ------子产品2
现在我们只需要更改 CanExpandGetter 和 ChildrenGetter:
// when the node can be expanded
tlvProduktStruktur.CanExpandGetter = 委托(对象 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>();
};
现在你应该得到预期的结果。