示例#1
0
        /// <summary>
        /// When overridden in a derived class, measures the size in layout
        /// required for child elements and determines a size for the System.Windows.FrameworkElement-derived class.
        /// </summary>
        /// <param name="availableSize">The available size that this element can give to child elements. Infinity can be specified as a value
        /// to indicate that the element will size to whatever content is available.</param>
        /// <returns>The size that this element determines it needs during layout, based on its calculations of child element sizes.</returns>
        protected override System.Windows.Size MeasureOverride(System.Windows.Size availableSize)
        {
            double totalHeight = 0;
            double maxWidth    = 0;

            if (!double.IsPositiveInfinity(availableSize.Width))
            {
                maxWidth = availableSize.Width;
            }
            List <UIElement> nonItemsElements = new List <UIElement>();

            foreach (object child in InternalChildren)
            {
                MenuItem item = child as MenuItem;
                if ((item != null) && (item.Visibility != Visibility.Collapsed))
                {
                    item.Measure(availableSize);
                    totalHeight += item.DesiredSize.Height;
                    maxWidth     = Math.Max(maxWidth, item.DesiredSize.Width);
                }
                else
                {
                    Separator separator = child as Separator;

                    if ((separator != null) && (separator.Visibility != Visibility.Collapsed))
                    {
                        separator.Measure(availableSize);
                        totalHeight += separator.DesiredSize.Height;
                        maxWidth     = Math.Max(maxWidth, separator.DesiredSize.Width);
                    }
                    else
                    {
                        UIElement uiElement = child as UIElement;
                        if (uiElement != null)
                        {
                            nonItemsElements.Add(uiElement);
                        }
                    }
                }
            }

            if ((!double.IsPositiveInfinity(availableSize.Height)))
            {
                if (totalHeight < availableSize.Height)
                {
                    double deltaHeight = (availableSize.Height - totalHeight) / nonItemsElements.Count;
                    foreach (FrameworkElement item in nonItemsElements)
                    {
                        if (item.Visibility != Visibility.Collapsed)
                        {
                            item.Measure(new Size(availableSize.Width, deltaHeight));
                            maxWidth     = Math.Max(maxWidth, Math.Max(item.DesiredSize.Width, (item).MinWidth));
                            totalHeight += Math.Max(item.DesiredSize.Height, (item).MinHeight);
                        }
                    }
                }
                else
                {
                    foreach (FrameworkElement item in nonItemsElements)
                    {
                        if (item.Visibility != Visibility.Collapsed)
                        {
                            item.Measure(new Size());
                            maxWidth     = Math.Max(maxWidth, Math.Max(item.DesiredSize.Width, item.MinWidth));
                            totalHeight += Math.Max(item.DesiredSize.Height, item.MinHeight);
                        }
                    }
                }
            }
            else
            {
                foreach (FrameworkElement item in nonItemsElements)
                {
                    if (item.Visibility != Visibility.Collapsed)
                    {
                        item.Measure(availableSize);
                        maxWidth     = Math.Max(maxWidth, Math.Max(item.DesiredSize.Width, item.MinWidth));
                        totalHeight += Math.Max(item.DesiredSize.Height, item.MinHeight);
                    }
                }
            }
            if (maxWidth < ResizeMinWidth)
            {
                maxWidth = ResizeMinWidth;
            }
            if (maxWidth > availableSize.Width)
            {
                maxWidth = availableSize.Width;
            }
            if (totalHeight < ResizeMinHeight)
            {
                totalHeight = ResizeMinHeight;
            }
            return(new Size(maxWidth, totalHeight));
        }