private static object CoerceDefaultControlSizeDefinition(DependencyObject d, object baseValue)
        {
            if (baseValue == null)
            {
                RibbonControlGroup controlGroup = (RibbonControlGroup)d;
                RibbonImageSize    imageSize    = RibbonImageSize.Collapsed;
                bool isLabelVisible             = false;
                int  itemCount  = controlGroup.Items.Count;
                bool childFound = false;

                // Get the largest ControlSizeDefinition variant for all
                // the child controls and construct a union ControlSizeDefinition.
                for (int i = 0; i < itemCount; i++)
                {
                    RibbonControl ribbonControl = controlGroup.ItemContainerGenerator.ContainerFromIndex(i) as RibbonControl;
                    if (ribbonControl != null && ribbonControl.Visibility != Visibility.Collapsed)
                    {
                        UIElement contentChild = ribbonControl.ContentChild;
                        if (contentChild != null && contentChild.Visibility != Visibility.Collapsed)
                        {
                            RibbonControlSizeDefinition currentLargeCsd = RibbonControlService.GetDefaultControlSizeDefinition(contentChild);
                            if (currentLargeCsd == null)
                            {
                                contentChild.CoerceValue(RibbonControlService.DefaultControlSizeDefinitionProperty);
                                currentLargeCsd = RibbonControlService.GetDefaultControlSizeDefinition(contentChild);
                            }

                            if (currentLargeCsd != null)
                            {
                                childFound = true;
                                if (imageSize == RibbonImageSize.Collapsed)
                                {
                                    imageSize = currentLargeCsd.ImageSize;
                                }
                                else if (currentLargeCsd.ImageSize == RibbonImageSize.Large)
                                {
                                    imageSize = RibbonImageSize.Large;
                                }

                                isLabelVisible |= currentLargeCsd.IsLabelVisible;

                                if (isLabelVisible && imageSize == RibbonImageSize.Large)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                if (childFound)
                {
                    return(RibbonControlSizeDefinition.GetFrozenControlSizeDefinition(imageSize, isLabelVisible));
                }
            }
            return(baseValue);
        }
 private static object CoerceControlSizeDefinition(DependencyObject d, object baseValue)
 {
     if (baseValue == null)
     {
         RibbonControlSizeDefinition defaultControlSizeDefinition = RibbonControlService.GetDefaultControlSizeDefinition(d);
         if (defaultControlSizeDefinition == null)
         {
             d.CoerceValue(DefaultControlSizeDefinitionProperty);
             defaultControlSizeDefinition = RibbonControlService.GetDefaultControlSizeDefinition(d);
         }
         return(defaultControlSizeDefinition);
     }
     return(baseValue);
 }