我尝试在树形结构中扩展多个列表。
我们说我有以下课程。我有类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;
};
--------------------------------------- < / p>
如果有人有兴趣,我找到了解决方案:
我的产品类有一个对象列表:
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);
结构如下所示:
rootProduct ------WorkingProcess -----------Article1 -----------Article2 ------SubProduct1 ------SubProduct2
现在我们只需要更改CanExpandGetter和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>();
};
现在你应该得到预期的结果。