Re: Re: Is there a way of easily create a dockpanel taking as base a stackpanel? Just to have the last child fill option
Hi Nuno,
Sorry I haven't replied, I've been really busy with work the past couple of days.
I've fixed the code and will post here since I don't have time to upload to my site. I'll do that later.
Below are the MeasureOverride and ArrangeOverride methods that have been fixed. I'll try to answer the rest of your questions later.
- jabb
protected override Size MeasureOverride(Size availableSize)
{
if (double.IsInfinity(availableSize.Width) || double.IsInfinity(availableSize.Height))
{
availableSize = new Size(0, 0);
foreach (UIElement child in Children)
child.Measure(availableSize);
return availableSize;
}
double width = availableSize.Width;
double height = availableSize.Height;
foreach (UIElement child in Children)
if (child.Visibility == Visibility.Visible)
{
child.Measure(new Size(width, height));
switch ((Dock)child.GetValue(DockProperty))
{
case Dock.Left:
case Dock.Right:
width = Math.Max(width - child.DesiredSize.Width, 0);
break;
case Dock.Top:
case Dock.Bottom:
height = Math.Max(height - child.DesiredSize.Height, 0);
break;
}
}
return new Size(
Math.Max(0, availableSize.Width - width),
Math.Max(0, availableSize.Height - height)
);
}
protected override Size ArrangeOverride(Size finalSize)
{
double left = 0;
double top = 0;
double width = finalSize.Width;
double height = finalSize.Height;
UIElement child;
for (int i = 0; i < Children.Count; i++)
{
child = Children
;
if (child.Visibility == Visibility.Visible)
{
if (lastChildFill && i == Children.Count - 1)
child.Arrange(new Rect(left, top, Math.Max(0, width - left), Math.Max(0, height - top)));
else
switch ((Dock)child.GetValue(DockProperty))
{
case Dock.Left:
child.Arrange(new Rect(left, top, child.DesiredSize.Width, Math.Max(0, height - top)));
left += child.DesiredSize.Width;
break;
case Dock.Top:
child.Arrange(new Rect(left, top, Math.Max(0, width - left), child.DesiredSize.Height));
top += child.DesiredSize.Height;
break;
case Dock.Right:
width -= child.DesiredSize.Width;
child.Arrange(new Rect(Math.Max(0, width), top, child.DesiredSize.Width, Math.Max(0, height - top)));
break;
case Dock.Bottom:
height -= child.DesiredSize.Height;
child.Arrange(new Rect(left, Math.Max(0, height), Math.Max(0, width - left), child.DesiredSize.Height));
break;
}
}
}
return finalSize;
}