Microsoft.Windows.Controls.Ribbon.RibbonGallery.PrepareContainerForItemOverride C# (CSharp) Method

PrepareContainerForItemOverride() protected method

Called when the container is being attached to the parent ItemsControl
protected PrepareContainerForItemOverride ( DependencyObject element, object item ) : void
element System.Windows.DependencyObject
item object
return void
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            RibbonGalleryCategory category = (RibbonGalleryCategory)element;
            category.RibbonGallery = this;

            if (SelectedValue != null && SelectedItem == null)
            {
                // Synchronize SelectedItem with SelectedValue
                ForceCoerceSelectedValue();
            }

            if (HasItems)
            {
                object selectedItem = SelectedItem;
                for (int index = 0; index < category.Items.Count; index++)
                {
                    RibbonGalleryItem galleryItem = category.ItemContainerGenerator.ContainerFromIndex(index) as RibbonGalleryItem;
                    if (galleryItem != null)
                    {
                        // Set IsSelected to true on GalleryItems that match the SelectedItem
                        if (selectedItem != null)
                        {
                            if (VerifyEqual(selectedItem, category.Items[index]))
                            {
                                galleryItem.IsSelected = true;
                            }
                        }
                        else if (galleryItem.IsSelected)
                        {
                            // If a GalleryItem is marked IsSelected true then synchronize SelectedItem with it
                            SelectedItem = category.Items[index];
                        }
                    }
                }
            }

            // copy templates and styles from this ItemsControl
            var itemTemplate = RibbonHelper.GetValueAndValueSource(category, ItemsControl.ItemTemplateProperty);
            var itemTemplateSelector = RibbonHelper.GetValueAndValueSource(category, ItemsControl.ItemTemplateSelectorProperty);
            var itemStringFormat = RibbonHelper.GetValueAndValueSource(category, ItemsControl.ItemStringFormatProperty);
            var itemContainerStyle = RibbonHelper.GetValueAndValueSource(category, ItemsControl.ItemContainerStyleProperty);
            var itemContainerStyleSelector = RibbonHelper.GetValueAndValueSource(category, ItemsControl.ItemContainerStyleSelectorProperty);
            var alternationCount = RibbonHelper.GetValueAndValueSource(category, ItemsControl.AlternationCountProperty);
            var itemBindingGroup = RibbonHelper.GetValueAndValueSource(category, ItemsControl.ItemBindingGroupProperty);

            base.PrepareContainerForItemOverride(element, item);

            // Call this function to work around a restriction of supporting hetrogenous
            // ItemsCotnrol hierarchy. The method takes care of both ItemsControl and
            // HeaderedItemsControl (in this case) and assign back the default properties
            // whereever appropriate.
            RibbonHelper.IgnoreDPInheritedFromParentItemsControl(
                category,
                this,
                itemTemplate,
                itemTemplateSelector,
                itemStringFormat,
                itemContainerStyle,
                itemContainerStyleSelector,
                alternationCount,
                itemBindingGroup,
                null,
                null,
                null);
        }
RibbonGallery