From a1c519c7c4e9b64ce6b201203f6abc77451912d0 Mon Sep 17 00:00:00 2001 From: ClemensF Date: Fri, 7 Dec 2012 17:04:44 +0100 Subject: [PATCH] Cleanup in MapPanel. --- MapControl/MapPanel.cs | 134 ++++++++++++++++++++++++++--------------- 1 file changed, 85 insertions(+), 49 deletions(-) diff --git a/MapControl/MapPanel.cs b/MapControl/MapPanel.cs index f267c596..9324c8b2 100644 --- a/MapControl/MapPanel.cs +++ b/MapControl/MapPanel.cs @@ -65,62 +65,28 @@ namespace MapControl foreach (UIElement element in Children) { + var rect = new Rect(0d, 0d, element.DesiredSize.Width, element.DesiredSize.Height); var location = GetLocation(element); + if (element is FrameworkElement) + { + if (location != null) + { + AlignElementWithLocation((FrameworkElement)element, ref rect); + } + else + { + AlignElementWithoutLocation((FrameworkElement)element, finalSize, ref rect); + } + } + + element.Arrange(rect); + if (location != null) { SetViewportPosition(element, parentMap, location); } - var rect = new Rect(0d, 0d, element.DesiredSize.Width, element.DesiredSize.Height); - var frameworkElement = element as FrameworkElement; - - if (frameworkElement != null) - { - switch (frameworkElement.HorizontalAlignment) - { - case HorizontalAlignment.Center: - rect.X = ((location == null ? finalSize.Width : 0) - rect.Width) / 2d; - break; - - case HorizontalAlignment.Right: - rect.X = (location == null ? finalSize.Width : 0) - rect.Width; - break; - - case HorizontalAlignment.Stretch: - if (location == null) - { - rect.Width = finalSize.Width; - } - break; - - default: - break; - } - - switch (frameworkElement.VerticalAlignment) - { - case VerticalAlignment.Center: - rect.Y = ((location == null ? finalSize.Height : 0) - rect.Height) / 2d; - break; - - case VerticalAlignment.Bottom: - rect.Y = (location == null ? finalSize.Height : 0) - rect.Height; - break; - - case VerticalAlignment.Stretch: - if (location == null) - { - rect.Height = finalSize.Height; - } - break; - - default: - break; - } - } - - element.Arrange(rect); } return finalSize; @@ -231,5 +197,75 @@ namespace MapControl element.ClearValue(UIElement.RenderTransformProperty); } } + + private static void AlignElementWithLocation(FrameworkElement element, ref Rect arrangeRect) + { + switch (element.HorizontalAlignment) + { + case HorizontalAlignment.Center: + arrangeRect.X = -arrangeRect.Width / 2d; + break; + + case HorizontalAlignment.Right: + arrangeRect.X = -arrangeRect.Width; + break; + + default: + break; + } + + switch (element.VerticalAlignment) + { + case VerticalAlignment.Center: + arrangeRect.Y = -arrangeRect.Height / 2d; + break; + + case VerticalAlignment.Bottom: + arrangeRect.Y = -arrangeRect.Height; + break; + + default: + break; + } + } + + private static void AlignElementWithoutLocation(FrameworkElement element, Size panelSize, ref Rect arrangeRect) + { + switch (element.HorizontalAlignment) + { + case HorizontalAlignment.Center: + arrangeRect.X = (panelSize.Width - arrangeRect.Width) / 2d; + break; + + case HorizontalAlignment.Right: + arrangeRect.X = panelSize.Width - arrangeRect.Width; + break; + + case HorizontalAlignment.Stretch: + arrangeRect.Width = panelSize.Width; + break; + + default: + break; + } + + switch (element.VerticalAlignment) + { + case VerticalAlignment.Center: + arrangeRect.Y = (panelSize.Height - arrangeRect.Height) / 2d; + break; + + case VerticalAlignment.Bottom: + arrangeRect.Y = panelSize.Height - arrangeRect.Height; + break; + + case VerticalAlignment.Stretch: + arrangeRect.Height = panelSize.Height; + break; + + default: + break; + } + } } }