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);
}