internal void RefreshWhenSave(CategoryViewModel viewModel) { if (viewModel.Model.Parent == null) { this.CategoryList.Add(viewModel); } else { TreeNodeModel parent = Tree.Find(this.CategoryList, (e) => (e as CategoryViewModel).Model.Id == (viewModel as CategoryViewModel).Model.Parent.Id) as TreeNodeModel; parent.AddChild(viewModel); } }
private void SetupAutoAdd(TreeNodeModel node) { node.PropertyChanged += (s, e) => { if (node.IsExpanded && node.Children.Count == 0) { for (var c = 0; c < 20; c++) { var ch = new TreeNodeModel { Name = "Auto-added child " + c }; SetupAutoAdd(ch); node.AddChild(ch); } TotalItems += 20; RaisePropertyChanged(nameof(TotalItems)); } }; }
void Populate(TreeNodeModel node, int levels) { if (levels == 0) { return; } for (var c = 0; c < 10; c++) { var ch = new TreeNodeModel { Name = "Child " + c, IsExpanded = levels > 1 }; if (levels == 1) { SetupAutoAdd(ch); } node.AddChild(ch); Populate(ch, levels - 1); } }
public static ObservableCollection <TreeNodeModel> BuildTree(IEnumerable <Category> col) { ObservableCollection <TreeNodeModel> tree = new ObservableCollection <TreeNodeModel>(); if (col != null && col.Count() > 0) { List <CategoryViewModel> list = (from item in col select new CategoryViewModel(item)) .ToList(); Queue <TreeNodeModel> queue = new Queue <TreeNodeModel>(); foreach (var item in list) { queue.Enqueue(item); } while (queue.Count > 0) { CategoryViewModel current = queue.Dequeue() as CategoryViewModel; if (string.IsNullOrWhiteSpace(current.Model.ParentId)) { tree.Add(current); } else { TreeNodeModel parent = Tree.Find(tree, (e) => (e as CategoryViewModel).Model.Id == current.Model.ParentId) as TreeNodeModel; if (parent == null) { queue.Enqueue(current); } else { parent.AddChild(current); } } } } return(tree); }