void Triggers_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.OldItems != null) { foreach (IBindingTrigger trigger in e.OldItems) { if (trigger.Device.Length > 0) { ProfileExplorerTreeItem folder = GetFolder(trigger.Device); folder.Children.Remove(folder.GetChildObject(trigger)); if (folder.Children.Count == 0) { Children.Remove(folder); } } else { Children.Remove(GetChildObject(trigger)); } } } if (e.NewItems != null) { foreach (IBindingTrigger trigger in e.NewItems) { AddTrigger(trigger, _includeTypes); } } }
void Actions_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.OldItems != null) { foreach (IBindingAction action in e.OldItems) { string deviceName = GetDeviceNameForUserInterface(action); if (deviceName.Length > 0) { ProfileExplorerTreeItem folder = GetFolder(deviceName); folder.Children.Remove(folder.GetChildObject(action)); if (folder.Children.Count == 0) { Children.Remove(folder); } } else { Children.Remove(GetChildObject(action)); } } } if (e.NewItems != null) { foreach (IBindingAction action in e.NewItems) { AddAction(action, _includeTypes); } } }
/// <summary> /// common code for handling collection changes to tree item collections that are represented as folders with items, /// where the folder name is dependent on the item (such as devices and their commands/values) /// </summary> /// <typeparam name="THeliosObject"></typeparam> /// <param name="removedChildren"></param> /// <param name="addedChildren"></param> /// <param name="folderChooser">function that returns null if the item should not be in the tree, a folder name if the item goes in a folder, and an empty string if the item should go directly under the current node</param> /// <param name="addItemHandler">function to call for each new item, has to handle the folder creation</param> private void StructuredContentsChanged <THeliosObject>(IEnumerable removedChildren, IEnumerable addedChildren, Func <THeliosObject, string> folderChooser, Action <THeliosObject, ProfileExplorerTreeItemType> addItemHandler) { if (removedChildren != null) { foreach (THeliosObject removedChild in removedChildren) { string folderName = folderChooser(removedChild); if (folderName == null) { // should not even be here continue; } ProfileExplorerTreeItem childItem; if (folderName.Length == 0) { childItem = GetChildObject(removedChild); if (childItem == null) { // not found continue; } Children.Remove(childItem); } else { ProfileExplorerTreeItem folder = GetFolder(folderName); if (folder == null) { // folder not found continue; } childItem = folder.GetChildObject(removedChild); if (childItem == null) { // child not in folder continue; } folder.Children.Remove(childItem); if (folder.Children.Count == 0) { // remove empty Children.Remove(folder); } } // now handle child removed childItem.Disconnect(); } } if (addedChildren == null) { return; } foreach (THeliosObject addedChild in addedChildren) { // handler will do the folder creation, if any addItemHandler(addedChild, _includeTypes); } }