internal void AddParent(Parent parent) { lock (_lock) { // Once locked, you can manipulate the collection safely from another thread ParentItems.Add((Parent)parent.Clone()); } }
private void LoadDummyParentItems() { for (var i = 0; i < 20; i++) { ParentItems.Add(new ParentItem() { Name = $"Parent Item {i}", CreatedOn = DateTime.Now.AddHours(i), IsActive = i % 2 == 0 ? true : false, Status = i % 2 == 0 ? ParentItemStatus.Status_Two : ParentItemStatus.Status_One, ChildItems = new List<string>() { $"Child one_{i}", $"Child two_{i}", $"Child three_{i}", $"Child four_{i}" } }); } }
public void ListToLevels(List <string> Values, List <int> Levels) { Element.Items.Clear(); for (int i = 0; i < Levels.Count; i++) { if (Levels[i] > 0) { TreeViewItem ParentItem = (TreeViewItem)Element.Items[Element.Items.Count - 1]; ObservableCollection <TreeViewItem> ParentItems = (ObservableCollection <TreeViewItem>)ParentItem.ItemsSource; if (ParentItems == null) { ParentItems = new ObservableCollection <TreeViewItem>(); } for (int j = 1; j < Levels[i]; j++) { ParentItem = (TreeViewItem)ParentItems[ParentItems.Count - 1]; ParentItems = (ObservableCollection <TreeViewItem>)ParentItem.ItemsSource; if (ParentItems == null) { ParentItems = new ObservableCollection <TreeViewItem>(); } } TreeViewItem SingleItem = new TreeViewItem(); SingleItem.Header = Values[i]; ParentItems.Add(SingleItem); ParentItem.ItemsSource = ParentItems; } else { TreeViewItem SingleItem = new TreeViewItem(); SingleItem.Header = Values[i]; Element.Items.Add(SingleItem); } } }