// Update all selection properties viz. // - SelectedItem // - SelectedValue // - CurrentItem // - IsSelected // - SelectedContainers internal void ChangeSelection(object item, RibbonGalleryItem container, bool isSelected) { if (IsSelectionChangeActive) { return; } object oldItem = SelectedItem; object newItem = item; bool selectedItemChanged = !VerifyEqual(oldItem, newItem); try { IsSelectionChangeActive = true; if (isSelected == selectedItemChanged) { // Deselecting a single container. This can only happen // when setting IsSelected to false on a specific container. // Note that neither SelectedItem nor CurrentItem are updated // in this case. We only updated the _selectedContainers and // ContainsSelection properties both of which are specific to // the containers in view. if (!isSelected && container != null) { container.IsSelected = false; int index = _selectedContainers.IndexOf(container); if (index > -1) { _selectedContainers.RemoveAt(index); container.OnUnselected(new RoutedEventArgs(RibbonGalleryItem.UnselectedEvent, container)); } } else { // This is the case where SelectedItem is changing. // We start the processing by deselecting all existing // containers. for (int i = 0; i < _selectedContainers.Count; i++) { RibbonGalleryItem galleryItem = _selectedContainers[i]; galleryItem.IsSelected = false; galleryItem.OnUnselected(new RoutedEventArgs(RibbonGalleryItem.UnselectedEvent, galleryItem)); if (!isSelected) { MoveCurrentToPosition(galleryItem.RibbonGalleryCategory.CollectionView, -1); } } _selectedContainers.Clear(); if (!isSelected) { SelectedItem = null; SelectedValue = null; MoveCurrentToPosition(CollectionView, -1); MoveCurrentToPosition(SourceCollectionView, -1); } } // Select the item if (isSelected) { SelectedItem = item; SelectedValue = GetSelectableValueFromItem(item); // Synchronize currency with the specified SelectedItem. if (container != null) { // This is the case where a single container is selected SynchronizeWithCurrentItem(container.RibbonGalleryCategory, item); } else { // This is the case where the selected item is directly // set in which case we need to additionally find the category // that contains it to perform the currency synchronization SynchronizeWithCurrentItem(); } } } // Select the container and synchronize currency if (isSelected && container != null && !_selectedContainers.Contains(container)) { _selectedContainers.Add(container); container.IsSelected = true; container.OnSelected(new RoutedEventArgs(RibbonGalleryItem.SelectedEvent, container)); } } finally { IsSelectionChangeActive = false; } if (selectedItemChanged) { RoutedPropertyChangedEventArgs<object> args = new RoutedPropertyChangedEventArgs<object>(oldItem, isSelected ? newItem : null, SelectionChangedEvent); this.OnSelectionChanged(args); } }