以下は可能な解決策です。 これは、ツリービューに便利なデータ型です。すべての
まず、あなたのCategory
クラス
// Your primary class
public class Category {
public Int32 CategoryId { get; set; }
public Int32? ParentCategoryId { get; set; }
public String CategoryName { get; set; }
// List of children if any.
// In this example if your category doesn't have any children
// it is supposed to be null
public List<Category> Children { get; set; }
// The parent category. If this is your topmost category
// this property is deemed to be null.
public Category Parent { get; set; }
public override String ToString() {
return this.CategoryName;
}
}
それは親への参照と子のリストを持って作成する必要があります。カテゴリが一番上の場合、Parent
プロパティはnull
です。子がない場合は、nullでもかまいません。
次に、指定されたラッパーが必要です。ここ は、彼らは私がダミーデータで上記のコードを使用しました
// your primary tree
public class CategoryTree : IHierarchicalEnumerable {
// Field to keep the collection data
private IEnumerable<Category> Categories;
// Empty constructor
public CategoryTree() {
//Init an empty list to avoid NullReferenceException
Categories = new List<Category>();
}
// Primary constructor of our interest
public CategoryTree(IEnumerable<Category> categories) {
// Assign the collection data to the internal list
Categories = categories;
}
public IHierarchyData GetHierarchyData(object enumeratedItem) {
//cast the object
var cat = enumeratedItem as Category;
//create a new hierarchy item
return new CategoryHierarchyItem(cat);
}
public System.Collections.IEnumerator GetEnumerator() {
//IEnumerable support for foreach loop
return Categories.GetEnumerator();
}
}
と
// Container that the TreeView control needs
public class CategoryHierarchyItem : IHierarchyData {
// Internal field to store your data
private Category _Item;
// the constructor used to instantiate the class
public CategoryHierarchyItem(Category input) {
this._Item = input;
}
// returns another tree with children if any
public IHierarchicalEnumerable GetChildren() {
if (_Item.Children != null)
return new CategoryTree(_Item.Children);
return new CategoryTree();
}
// The boolean value indicating weather this is the deepest level of your
// categories
public Boolean HasChildren {
get { return _Item.Children != null; }
}
// Actual data item
public Object Item {
get { return _Item; }
}
// This is the path.
// Construct it as you think is necessary.
// Here the format is the same as in namespaces of C#
public String Path {
get {
var sb = new StringBuilder();
var current = _Item;
sb.Append(_Item.CategoryName);
while (current != null) {
sb.Insert(0, ".");
sb.Insert(0, current.CategoryName);
current = current.Parent;
}
return sb.ToString();
}
}
public String Type {
get { return "Category"; }
}
// returns the parent of the current item
public IHierarchyData GetParent() {
if (_Item.Parent != null)
return new CategoryHierarchyItem(_Item.Parent);
return null;
}
}
です。これはほぼ同じように見えます。
protected void Page_Load(object sender, EventArgs e) {
// The topmost category
var top = new Category() {
CategoryId = 1,
CategoryName = "Top category"
};
// Prepare children for them
var children = new List<Category>();
// assign the children
top.Children = children;
// Populate the child list
children.Add(new Category {
CategoryId = 2,
CategoryName = "First child",
Parent = top
});
children.Add(new Category {
CategoryId = 3,
CategoryName = "Second child",
Parent = top
});
//Add another sub child
var thirdLevel = new List<Category>();
thirdLevel.Add(new Category {
CategoryId = 4,
CategoryName = "Sub sub category",
Parent = children[0]
});
// assign it to the first child
children[0].Children = thirdLevel;
// Make up a new tree
var tree = new CategoryTree(new[] { top });
//Bind the source.
TreeView1.DataSource = tree;
// Bind it and enjoy.
TreeView1.DataBind();
}
可能な限りソリューションを変更できます。 ロジックをデータアクセスとUIツリービルディングに分割する必要があります。
これは、 'TreeView'コントロールにバインドするデータは階層型でなければならないことを意味し、親ノードと子ノードを含む必要があります。例えば、 'XmlDataSource'かもしれません。 – Samich
多くの場合、_ "絵は千語の価値がある"と言うことができる。しかし、このケースでは、代わりにあなたのコードの関連する部分を表示する方がはるかに優れています... – Nailuj