diff --git a/MapControl/MapControl.csproj b/MapControl/MapControl.csproj index 4c666b88..dce34dd9 100644 --- a/MapControl/MapControl.csproj +++ b/MapControl/MapControl.csproj @@ -68,7 +68,6 @@ - diff --git a/MapControl/MapElement.cs b/MapControl/MapElement.cs index 9511bc1e..8134ff90 100644 --- a/MapControl/MapElement.cs +++ b/MapControl/MapElement.cs @@ -19,12 +19,6 @@ namespace MapControl new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, ParentMapPropertyChanged)); } - protected MapElement() - { - HorizontalAlignment = HorizontalAlignment.Stretch; - VerticalAlignment = VerticalAlignment.Stretch; - } - public Map ParentMap { get { return MapPanel.GetParentMap(this); } diff --git a/MapControl/MapItemsControl.cs b/MapControl/MapItemsControl.cs index 24ad37aa..e0127d97 100644 --- a/MapControl/MapItemsControl.cs +++ b/MapControl/MapItemsControl.cs @@ -128,12 +128,12 @@ namespace MapControl } else if ((selectedContainer = GetContainer(SelectedItem)) != null) { - ViewportPosition p1 = MapPanel.GetViewportPosition(selectedContainer); - ViewportPosition p2 = MapPanel.GetViewportPosition(container); + Point? p1 = MapPanel.GetViewportPosition(selectedContainer); + Point? p2 = MapPanel.GetViewportPosition(container); - if (p1 != null && p2 != null) + if (p1.HasValue && p2.HasValue) { - Rect rect = new Rect(p1.Position, p2.Position); + Rect rect = new Rect(p1.Value, p2.Value); BeginUpdateSelectedItems(); SelectedItems.Clear(); @@ -237,21 +237,21 @@ namespace MapControl private bool IsItemInGeometry(object item, Geometry geometry) { UIElement container = GetContainer(item); - ViewportPosition viewportPosition; + Point? viewportPosition; return container != null - && (viewportPosition = MapPanel.GetViewportPosition(container)) != null - && geometry.FillContains(viewportPosition.Position); + && (viewportPosition = MapPanel.GetViewportPosition(container)).HasValue + && geometry.FillContains(viewportPosition.Value); } private bool IsItemInRect(object item, Rect rect) { UIElement container = GetContainer(item); - ViewportPosition viewportPosition; + Point? viewportPosition; return container != null - && (viewportPosition = MapPanel.GetViewportPosition(container)) != null - && rect.Contains(viewportPosition.Position); + && (viewportPosition = MapPanel.GetViewportPosition(container)).HasValue + && rect.Contains(viewportPosition.Value); } } } diff --git a/MapControl/MapPanel.cs b/MapControl/MapPanel.cs index ad3881fc..03a3c564 100644 --- a/MapControl/MapPanel.cs +++ b/MapControl/MapPanel.cs @@ -4,15 +4,13 @@ using System.Windows; using System.Windows.Controls; -using System.Windows.Media; namespace MapControl { /// /// Positions child elements on a Map. A child element's position is specified by the /// attached property Location, given as geographic location with latitude and longitude. - /// The attached property ViewportPosition gets a child element's position in viewport - /// coordinates and indicates if the coordinates are located inside the bounds of the ParentMap. + /// The attached property ViewportPosition gets a child element's position in viewport coordinates. /// public class MapPanel : Panel { @@ -23,7 +21,7 @@ namespace MapControl public static readonly DependencyProperty ParentMapProperty = ParentMapPropertyKey.DependencyProperty; private static readonly DependencyPropertyKey ViewportPositionPropertyKey = DependencyProperty.RegisterAttachedReadOnly( - "ViewportPosition", typeof(ViewportPosition), typeof(MapPanel), + "ViewportPosition", typeof(Point?), typeof(MapPanel), new FrameworkPropertyMetadata(ViewportPositionPropertyChanged)); public static readonly DependencyProperty ViewportPositionProperty = ViewportPositionPropertyKey.DependencyProperty; @@ -47,9 +45,9 @@ namespace MapControl return (Map)element.GetValue(ParentMapProperty); } - public static ViewportPosition GetViewportPosition(UIElement element) + public static Point? GetViewportPosition(UIElement element) { - return (ViewportPosition)element.GetValue(ViewportPositionProperty); + return (Point?)element.GetValue(ViewportPositionProperty); } public static Location GetLocation(UIElement element) @@ -78,22 +76,21 @@ namespace MapControl { foreach (UIElement element in InternalChildren) { - ViewportPosition viewportPosition = GetViewportPosition(element); + Point? viewportPosition = GetViewportPosition(element); - if (viewportPosition == null || !ArrangeElement(element, viewportPosition)) + if (viewportPosition.HasValue) { - ArrangeElement(element, finalSize); + ArrangeElement(element, viewportPosition.Value); + } + else + { + element.Arrange(new Rect(finalSize)); } } return finalSize; } - protected virtual Point GetArrangePosition(ViewportPosition viewportPosition) - { - return viewportPosition.Position; - } - protected virtual void OnViewportChanged() { Map parentMap = ParentMap; @@ -136,11 +133,11 @@ namespace MapControl if (element != null) { - ViewportPosition position = (ViewportPosition)e.NewValue; + Point? viewportPosition = (Point?)e.NewValue; - if (position != null) + if (viewportPosition.HasValue) { - ArrangeElement(element, position); + ArrangeElement(element, viewportPosition.Value); } else { @@ -161,36 +158,23 @@ namespace MapControl private static void SetViewportPosition(UIElement element, Map parentMap, Location location) { - ViewportPosition viewportPosition = null; + Point? viewportPosition = null; if (parentMap != null && location != null) { - Point position = parentMap.LocationToViewportPoint(location); - - viewportPosition = new ViewportPosition(position, - position.X >= 0d && position.X <= parentMap.ActualWidth && - position.Y >= 0d && position.Y <= parentMap.ActualHeight); + viewportPosition = parentMap.LocationToViewportPoint(location); } element.SetValue(ViewportPositionPropertyKey, viewportPosition); } - private static bool ArrangeElement(UIElement element, ViewportPosition viewportPosition) + private static void ArrangeElement(UIElement element, Point position) { - MapPanel panel = VisualTreeHelper.GetParent(element) as MapPanel; - Point position = panel != null ? panel.GetArrangePosition(viewportPosition) : viewportPosition.Position; Rect rect = new Rect(position, element.DesiredSize); - FrameworkElement frameworkElement = element as FrameworkElement; - if (frameworkElement != null) + if (element is FrameworkElement) { - if (frameworkElement.HorizontalAlignment == HorizontalAlignment.Stretch || - frameworkElement.VerticalAlignment == VerticalAlignment.Stretch) - { - return false; // do not arrange at position - } - - switch (frameworkElement.HorizontalAlignment) + switch (((FrameworkElement)element).HorizontalAlignment) { case HorizontalAlignment.Center: rect.X -= rect.Width / 2d; @@ -198,11 +182,14 @@ namespace MapControl case HorizontalAlignment.Right: rect.X -= rect.Width; break; + case HorizontalAlignment.Stretch: + rect.X = 0d; + break; default: break; } - switch (frameworkElement.VerticalAlignment) + switch (((FrameworkElement)element).VerticalAlignment) { case VerticalAlignment.Center: rect.Y -= rect.Height / 2d; @@ -210,47 +197,8 @@ namespace MapControl case VerticalAlignment.Bottom: rect.Y -= rect.Height; break; - default: - break; - } - } - - element.Arrange(rect); - return true; - } - - private static void ArrangeElement(UIElement element, Size panelSize) - { - Rect rect = new Rect(element.DesiredSize); - FrameworkElement frameworkElement = element as FrameworkElement; - - if (frameworkElement != null) - { - switch (frameworkElement.HorizontalAlignment) - { - case HorizontalAlignment.Center: - rect.X = (panelSize.Width - rect.Width) / 2d; - break; - case HorizontalAlignment.Right: - rect.X = panelSize.Width - rect.Width; - break; - case HorizontalAlignment.Stretch: - rect.Width = panelSize.Width; - break; - default: - break; - } - - switch (frameworkElement.VerticalAlignment) - { - case VerticalAlignment.Center: - rect.Y = (panelSize.Height - rect.Height) / 2d; - break; - case VerticalAlignment.Bottom: - rect.Y = panelSize.Height - rect.Height; - break; case VerticalAlignment.Stretch: - rect.Height = panelSize.Height; + rect.Y = 0d; break; default: break; diff --git a/MapControl/Themes/Generic.xaml b/MapControl/Themes/Generic.xaml index 1409c6d7..9eb6b1fe 100644 --- a/MapControl/Themes/Generic.xaml +++ b/MapControl/Themes/Generic.xaml @@ -11,6 +11,8 @@