/// <summary> /// Updates the Breadcrumb list and resets all the leaves (including the active one). /// </summary> /// <param name="group">The group to activate.</param> public void SetGroup(IKeePassGroup group) { // If this is an effective no-op, do nothing. if (group == ActiveGroup) { return; } int originalChildCount = 0; // Remove the children changed handler from the current group. if (ActiveGroup != null) { ((INotifyCollectionChanged)ActiveGroup.Children).CollectionChanged -= ChildrenChangedHandler; originalChildCount = ActiveGroup.Children.Count; } // Are we clearing everything? if (group == null) { this.breadcrumbs.Clear(); } else { // Are we navigating upwards from the current tree? if (ActiveGroup != null && ActiveGroup.HasAncestor(group)) { int breadcrumbLastIndex = this.breadcrumbs.Count - 1; // Pop off breadcrumbs until we're done while (this.breadcrumbs[breadcrumbLastIndex].Group != group) { this.breadcrumbs.RemoveAt(breadcrumbLastIndex--); } } // Are we navigating down a level to a new child of the current Group? else if (group.Parent != null && group.Parent.Equals(ActiveGroup)) { this.breadcrumbs.Add(new Breadcrumb(group)); } // Something more catastrophic is happened, so just reset the list else { this.breadcrumbs.Clear(); while (group != null) { this.breadcrumbs.Insert(0, new Breadcrumb(group, (group.Parent == null))); group = group.Parent; } } ((INotifyCollectionChanged)ActiveGroup.Children).CollectionChanged += ChildrenChangedHandler; } if (ActiveLeaf != null && group != ActiveLeaf.Parent) { // Remove the ActiveLeaf if we're changing the ActiveGroup ActiveLeaf = null; } OnPropertyChanged(nameof(ActiveGroup)); if (originalChildCount != 0 || (ActiveGroup != null && ActiveGroup.Children.Count != 0)) { RaiseLeavesChanged(); } }