//resets the items source private void ResetItemsSource(IEnumerable newValue) { if (newValue != null) { compositeChildCollection.Clear(); if (rootNodesInfo == null) { rootNodesInfo = new List <TreeListBoxInfo>(); } else { rootNodesInfo.Clear(); } foreach (object item in newValue) { TreeListBoxInfo info = new TreeListBoxInfo(0, item); //add the new info to the main flattened list compositeChildCollection.Add(info); // add the new info to intenal list of data items rootNodesInfo.Add(info); } //set the items sources to be out compositeChildCollection that contains all the VirtualizingTreeViewInfo ItemsSource = compositeChildCollection; } else { ItemsSource = null;// if the new source is null set the source to be null } }
//event handler for the collection change of the items source void NotificationEnableCollectionCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: if (e.NewItems != null) { int counter = 0; foreach (object item in e.NewItems) { //get the destination index int originalDestinationIndex = Math.Min(e.NewStartingIndex + counter, rootNodesInfo.Count); //walk through the list of ancestors and count their children int newDestinationIndex = 0; for (int i = originalDestinationIndex - 1; i >= 0; i--) { newDestinationIndex += (rootNodesInfo[i].ChildrenCount + 1); } originalDestinationIndex = newDestinationIndex; //if the destination is 0 you still need to add 1 //create the new list item TreeListBoxInfo newInfo = new TreeListBoxInfo(0, item); //add the new item in the internal list rootNodesInfo.Insert(Math.Min(e.NewStartingIndex + counter, rootNodesInfo.Count), newInfo); //add to the main list(the flattened list) compositeChildCollection.Insert(originalDestinationIndex, newInfo); counter++; } } break; case NotifyCollectionChangedAction.Remove: if (compositeChildCollection.Count > e.OldStartingIndex) { //remove all children TreeListBoxInfo info = rootNodesInfo[e.OldStartingIndex]; //remove all child items for (int i = 0; i < info.ChildrenCount; i++) { compositeChildCollection.RemoveAt(e.OldStartingIndex); } //remove the actual item compositeChildCollection.RemoveAt(e.OldStartingIndex); rootNodesInfo.RemoveAt(e.OldStartingIndex); } break; case NotifyCollectionChangedAction.Move: case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Reset: //reset the items source ResetItemsSource(sender as IEnumerable); break; } }
/// <summary> /// Prepares the new VirtualizingTreeViewItem with the actual business object /// </summary> /// <param name="element">The element (VirtualizingTreeViewItem) to apply the template</param> /// <param name="item">The business object to set</param> protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { TreeListBoxItem listItem = element as TreeListBoxItem; TreeListBoxInfo actualItem = item as TreeListBoxInfo; //raise the event to signal that a new item is created OnNewItemCreated(new TreeListBoxItemCreatedEventArgs(listItem, actualItem)); //prepares the item with the relative VirtualizingTreeViewInfo listItem.PrepareItem(actualItem); //pass the actual data item instead of the VirtualizingTreeViewInfo base.PrepareContainerForItemOverride(element, actualItem.DataItem); }
//generate a single TreeListBoxItem private void GenerateSingleItem(object item) { //get the current index. TODO done once int currentIndex = treeListBox.CompositeChildCollection.IndexOf(currentInfo); if (currentIndex != -1) { //create a child list item TreeListBoxInfo newObject = new TreeListBoxInfo(level + 1, item); currentInfo.ChildItems.Add(newObject); treeListBox.CompositeChildCollection.Insert( currentIndex + lastChildItemCreatedIndex, newObject); lastChildItemCreatedIndex++; } }
/// <summary> /// Prepares the item by setting the properties /// Override this method if you want to attach any extra info to the item /// </summary> /// <param name="info">The info to apply</param> internal virtual void PrepareItem(TreeListBoxInfo info) { //set the current info for this object currentInfo = info; //set the level level = info.Level; //check if the item was expanded. //you need to reset this propety since the Virtualization can delete the instance of this item and after recreate it if (info.IsExpanded) { IsExpanded = true; } isInitializing = false; }
//gets the child count by walking the children tree private static int GetChildCount(TreeListBoxInfo info) { if (info.childItems == null || info.childItems.Count == 0) { return(0); } int childCount = 0; foreach (TreeListBoxInfo child in info.childItems) { childCount += GetChildCount(child); // add the child count childCount++; //add the current item } return(childCount); }
//delets a single item from the list private bool DeleteSingleItem(TreeListBoxInfo info) { int index = treeListBox.CompositeChildCollection.IndexOf(info); if (index != -1) { //remove all children for (int i = 0; i < info.ChildrenCount; i++) { treeListBox.CompositeChildCollection[index].IsExpanded = false; treeListBox.CompositeChildCollection.RemoveAt(index); } treeListBox.CompositeChildCollection[index].IsExpanded = false; treeListBox.CompositeChildCollection.RemoveAt(index);//remove itself currentInfo.ChildItems.Remove(info); lastChildItemCreatedIndex--; return(true); } return(false); }
/// <summary> /// Constructor /// </summary> /// <param name="item">The new item created</param> /// <param name="info">The info relative to this item</param> public TreeListBoxItemCreatedEventArgs(TreeListBoxItem item, TreeListBoxInfo info) { newItem = item; newItemInfo = info; }
//event handler for databinding notification fo children private void NotificationEnabledCollectionCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { //if the expanded is false. Unregister to events and exit //this can happen if the Drop all does not unregister to notification events if (currentInfo.IsExpanded == false) { UnRegisterCollectionNotification(); return; } switch (e.Action) { //new item(s) where added case NotifyCollectionChangedAction.Add: if (e.NewItems != null) { int counter = 0; foreach (object item in e.NewItems) { int originalDestinationIndex = Math.Min(e.NewStartingIndex + counter, currentInfo.ChildItems.Count); //get the index for the main list (the flattened list) int newDestinationIndex = 1; //walk through all ancestor items and get te child count of the items for (int i = originalDestinationIndex - 1; i >= 0; i--) { //get the child count of the item + 1 which is the item itself newDestinationIndex += (currentInfo.ChildItems[i].ChildrenCount + 1); } originalDestinationIndex = newDestinationIndex + // the new destination treeListBox.CompositeChildCollection.IndexOf(currentInfo); // the index of the current item //create the new list item TreeListBoxInfo newInfo = new TreeListBoxInfo(level + 1, item); currentInfo.ChildItems.Insert( Math.Min(e.NewStartingIndex + counter, currentInfo.ChildItems.Count), newInfo); treeListBox.CompositeChildCollection.Insert(originalDestinationIndex, newInfo); counter++; } } break; //item was removed case NotifyCollectionChangedAction.Remove: if (currentInfo.ChildItems.Count > e.OldStartingIndex) { DeleteSingleItem(currentInfo.ChildItems[e.OldStartingIndex]); } break; case NotifyCollectionChangedAction.Move: case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Reset: DropAllListItems(); GenerateAllListItems(); break; } }
//gets the child count by walking the children tree private static int GetChildCount(TreeListBoxInfo info) { if (info.childItems == null || info.childItems.Count == 0) return 0; int childCount = 0; foreach (TreeListBoxInfo child in info.childItems) { childCount += GetChildCount(child); // add the child count childCount++;//add the current item } return childCount; }
//generate a single TreeListBoxItem private void GenerateSingleItem(object item) { //get the current index. TODO done once int currentIndex = treeListBox.CompositeChildCollection.IndexOf(currentInfo); if (currentIndex != -1) { //create a child list item TreeListBoxInfo newObject = new TreeListBoxInfo(level + 1, item); currentInfo.ChildItems.Add(newObject); treeListBox.CompositeChildCollection.Insert( currentIndex + lastChildItemCreatedIndex, newObject); lastChildItemCreatedIndex++; } }
//event handler for databinding notification fo children private void NotificationEnabledCollectionCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { //if the expanded is false. Unregister to events and exit //this can happen if the Drop all does not unregister to notification events if (currentInfo.IsExpanded == false) { UnRegisterCollectionNotification(); return; } switch (e.Action) { //new item(s) where added case NotifyCollectionChangedAction.Add: if (e.NewItems != null) { int counter = 0; foreach (object item in e.NewItems) { int originalDestinationIndex = Math.Min(e.NewStartingIndex + counter, currentInfo.ChildItems.Count); //get the index for the main list (the flattened list) int newDestinationIndex = 1; //walk through all ancestor items and get te child count of the items for (int i = originalDestinationIndex - 1; i >= 0; i--) //get the child count of the item + 1 which is the item itself newDestinationIndex += (currentInfo.ChildItems[i].ChildrenCount + 1); originalDestinationIndex = newDestinationIndex + // the new destination treeListBox.CompositeChildCollection.IndexOf(currentInfo);// the index of the current item //create the new list item TreeListBoxInfo newInfo = new TreeListBoxInfo(level + 1, item); currentInfo.ChildItems.Insert( Math.Min(e.NewStartingIndex + counter, currentInfo.ChildItems.Count), newInfo); treeListBox.CompositeChildCollection.Insert(originalDestinationIndex, newInfo); counter++; } } break; //item was removed case NotifyCollectionChangedAction.Remove: if (currentInfo.ChildItems.Count > e.OldStartingIndex) DeleteSingleItem(currentInfo.ChildItems[e.OldStartingIndex]); break; case NotifyCollectionChangedAction.Move: case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Reset: DropAllListItems(); GenerateAllListItems(); break; } }
/// <summary> /// Prepares the item by setting the properties /// Override this method if you want to attach any extra info to the item /// </summary> /// <param name="info">The info to apply</param> internal virtual void PrepareItem(TreeListBoxInfo info) { //set the current info for this object currentInfo = info; //set the level level = info.Level; //check if the item was expanded. //you need to reset this propety since the Virtualization can delete the instance of this item and after recreate it if (info.IsExpanded) IsExpanded = true; isInitializing = false; }
public void ExposePrepareItem(TreeListBoxInfo info) { PrepareItem(info); }
//resets the items source private void ResetItemsSource(IEnumerable newValue) { if (newValue != null) { compositeChildCollection.Clear(); if (rootNodesInfo == null) rootNodesInfo = new List<TreeListBoxInfo>(); else rootNodesInfo.Clear(); foreach (object item in newValue) { TreeListBoxInfo info = new TreeListBoxInfo(0, item); //add the new info to the main flattened list compositeChildCollection.Add(info); // add the new info to intenal list of data items rootNodesInfo.Add(info); } //set the items sources to be out compositeChildCollection that contains all the VirtualizingTreeViewInfo ItemsSource = compositeChildCollection; } else ItemsSource = null;// if the new source is null set the source to be null }
//event handler for the collection change of the items source void NotificationEnableCollectionCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: if (e.NewItems != null) { int counter = 0; foreach (object item in e.NewItems) { //get the destination index int originalDestinationIndex = Math.Min(e.NewStartingIndex + counter, rootNodesInfo.Count); //walk through the list of ancestors and count their children int newDestinationIndex = 0; for (int i = originalDestinationIndex - 1; i >= 0; i--) newDestinationIndex += (rootNodesInfo[i].ChildrenCount + 1); originalDestinationIndex = newDestinationIndex;//if the destination is 0 you still need to add 1 //create the new list item TreeListBoxInfo newInfo = new TreeListBoxInfo(0, item); //add the new item in the internal list rootNodesInfo.Insert(Math.Min(e.NewStartingIndex + counter, rootNodesInfo.Count), newInfo); //add to the main list(the flattened list) compositeChildCollection.Insert(originalDestinationIndex, newInfo); counter++; } } break; case NotifyCollectionChangedAction.Remove: if (compositeChildCollection.Count > e.OldStartingIndex) { //remove all children TreeListBoxInfo info = rootNodesInfo[e.OldStartingIndex]; //remove all child items for (int i = 0; i < info.ChildrenCount; i++) compositeChildCollection.RemoveAt(e.OldStartingIndex); //remove the actual item compositeChildCollection.RemoveAt(e.OldStartingIndex); rootNodesInfo.RemoveAt(e.OldStartingIndex); } break; case NotifyCollectionChangedAction.Move: case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Reset: //reset the items source ResetItemsSource(sender as IEnumerable); break; } }
//delets a single item from the list private bool DeleteSingleItem(TreeListBoxInfo info) { int index = treeListBox.CompositeChildCollection.IndexOf(info); if (index != -1) { //remove all children for (int i = 0; i < info.ChildrenCount; i++) { treeListBox.CompositeChildCollection[index].IsExpanded = false; treeListBox.CompositeChildCollection.RemoveAt(index); } treeListBox.CompositeChildCollection[index].IsExpanded = false; treeListBox.CompositeChildCollection.RemoveAt(index);//remove itself currentInfo.ChildItems.Remove(info); lastChildItemCreatedIndex--; return true; } return false; }
/// <summary> /// Constructor /// </summary> /// <param name="item">The new item created</param> /// <param name="info">The info relative to this item</param> public TreeListBoxItemCreatedEventArgs(TreeListBoxItem item, TreeListBoxInfo info) { newItem = item; newItemInfo = info; }
public void ExposePrepareItem(TreeListBoxInfo info) { PrepareItem(info); }