/// <summary> /// Called when the container is being attached to the parent ItemsControl /// </summary> /// <param name="element"></param> /// <param name="item"></param> protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { RibbonGalleryItem galleryItem = (RibbonGalleryItem)element; galleryItem.RibbonGalleryCategory = this; RibbonGallery gallery = RibbonGallery; if (gallery != null) { object selectedItem = gallery.SelectedItem; if (selectedItem != null) { // Set IsSelected to true on GalleryItems that match the SelectedItem if (RibbonGallery.VerifyEqual(item, selectedItem)) { galleryItem.IsSelected = true; } } else if (galleryItem.IsSelected) { // If a GalleryItem is marked IsSelected true then synchronize SelectedItem with it gallery.SelectedItem = item; } else { object selectedValue = gallery.SelectedValue; if (selectedValue != null) { // Set SelectedItem if the item's value matches the SelectedValue object itemValue = gallery.GetSelectableValueFromItem(item); if (RibbonGallery.VerifyEqual(selectedValue, itemValue)) { galleryItem.IsSelected = true; } } } } galleryItem.SyncKeyTipAndContent(); base.PrepareContainerForItemOverride(element, item); }