Added MapPanel.AutoCollapse property

This commit is contained in:
ClemensF 2021-01-13 00:08:55 +01:00
parent ec1a018b50
commit fead75ffb9
11 changed files with 138 additions and 83 deletions

View file

@ -33,12 +33,27 @@ namespace MapControl
MapPanel.InitMapElement(this);
}
/// <summary>
/// Wrapper for the MapPanel.AutoCollapse attached property.
/// </summary>
public bool AutoCollapse
{
get { return (bool)GetValue(AutoCollapseProperty); }
set { SetValue(AutoCollapseProperty, value); }
}
/// <summary>
/// Wrapper for the MapPanel.Location attached property.
/// </summary>
public Location Location
{
get { return (Location)GetValue(LocationProperty); }
set { SetValue(LocationProperty, value); }
}
/// <summary>
/// Path to a source property for binding the Location property.
/// </summary>
public string LocationMemberPath
{
get { return (string)GetValue(LocationMemberPathProperty); }

View file

@ -31,13 +31,8 @@ namespace MapControl
/// </summary>
public partial class MapPanel : Panel, IMapElement
{
private static void ParentMapPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
if (obj is IMapElement mapElement)
{
mapElement.ParentMap = e.NewValue as MapBase;
}
}
public static readonly DependencyProperty AutoCollapseProperty = DependencyProperty.RegisterAttached(
"AutoCollapse", typeof(bool), typeof(MapPanel), new PropertyMetadata(false));
private MapBase parentMap;
@ -52,26 +47,58 @@ namespace MapControl
InitMapElement(this);
}
/// <summary>
/// Gets a value that controls whether an element's Visibility is automatically
/// set to Collapsed when it is located outside the visible viewport area.
/// </summary>
public static bool GetAutoCollapse(FrameworkElement element)
{
return (bool)element.GetValue(AutoCollapseProperty);
}
/// <summary>
/// Sets the AutoCollapse property.
/// </summary>
public static void SetAutoCollapse(FrameworkElement element, bool value)
{
element.SetValue(AutoCollapseProperty, value);
}
/// <summary>
/// Gets the geodetic Location of an element.
/// </summary>
public static Location GetLocation(FrameworkElement element)
{
return (Location)element.GetValue(LocationProperty);
}
/// <summary>
/// Sets the geodetic Location of an element.
/// </summary>
public static void SetLocation(FrameworkElement element, Location value)
{
element.SetValue(LocationProperty, value);
}
/// <summary>
/// Gets the BoundingBox of an element.
/// </summary>
public static BoundingBox GetBoundingBox(FrameworkElement element)
{
return (BoundingBox)element.GetValue(BoundingBoxProperty);
}
/// <summary>
/// Sets the BoundingBox of an element.
/// </summary>
public static void SetBoundingBox(FrameworkElement element, BoundingBox value)
{
element.SetValue(BoundingBoxProperty, value);
}
/// <summary>
/// Gets the position of an element in view coordinates.
/// </summary>
public static Point? GetViewPosition(FrameworkElement element)
{
return (Point?)element.GetValue(ViewPositionProperty);
@ -80,8 +107,13 @@ namespace MapControl
/// <summary>
/// Returns the view position of a Location.
/// </summary>
public Point GetViewPosition(Location location)
public Point? GetViewPosition(Location location)
{
if (location == null)
{
return null;
}
var pos = parentMap.LocationToView(location);
if (parentMap.MapProjection.IsNormalCylindrical &&
@ -175,19 +207,32 @@ namespace MapControl
{
foreach (var element in Children.OfType<FrameworkElement>())
{
var location = GetLocation(element);
var position = GetViewPosition(GetLocation(element));
if (location != null)
SetViewPosition(element, position);
if (GetAutoCollapse(element))
{
var position = GetViewPosition(location);
if (position.HasValue &&
(position.Value.X < 0d ||
position.Value.Y < 0d ||
position.Value.X > parentMap.RenderSize.Width ||
position.Value.Y > parentMap.RenderSize.Height))
{
element.SetValue(VisibilityProperty, Visibility.Collapsed);
}
else
{
element.ClearValue(VisibilityProperty);
}
}
SetViewPosition(element, position);
ArrangeElement(element, position);
if (position.HasValue)
{
ArrangeElement(element, position.Value);
}
else
{
SetViewPosition(element, null);
var boundingBox = GetBoundingBox(element);
if (boundingBox != null)
@ -314,5 +359,13 @@ namespace MapControl
element.Arrange(rect);
}
private static void ParentMapPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
if (obj is IMapElement mapElement)
{
mapElement.ParentMap = e.NewValue as MapBase;
}
}
}
}

View file

@ -29,6 +29,15 @@ namespace MapControl
MapPanel.InitMapElement(this);
}
/// <summary>
/// Wrapper for the MapPanel.AutoCollapse attached property.
/// </summary>
public bool AutoCollapse
{
get { return (bool)GetValue(AutoCollapseProperty); }
set { SetValue(AutoCollapseProperty, value); }
}
/// <summary>
/// Gets or sets a Location that is used as
/// - either the origin point of a geometry specified in cartesian map units (meters)

View file

@ -17,13 +17,18 @@ namespace MapControl
/// </summary>
public class Pushpin : ContentControl
{
public static readonly DependencyProperty LocationProperty =
#if WINDOWS_UWP
DependencyProperty.Register(
nameof(Location), typeof(Location), typeof(Pushpin),
new PropertyMetadata(null, (o, e) => MapPanel.SetLocation((FrameworkElement)o, (Location)e.NewValue)));
public static readonly DependencyProperty AutoCollapseProperty = DependencyProperty.Register(
nameof(AutoCollapse), typeof(bool), typeof(Pushpin),
new PropertyMetadata(false, (o, e) => MapPanel.SetAutoCollapse((FrameworkElement)o, (bool)e.NewValue)));
public static readonly DependencyProperty LocationProperty = DependencyProperty.Register(
nameof(Location), typeof(Location), typeof(Pushpin),
new PropertyMetadata(null, (o, e) => MapPanel.SetLocation((FrameworkElement)o, (Location)e.NewValue)));
#else
MapPanel.LocationProperty.AddOwner(typeof(Pushpin));
public static readonly DependencyProperty AutoCollapseProperty = MapPanel.AutoCollapseProperty.AddOwner(typeof(Pushpin));
public static readonly DependencyProperty LocationProperty = MapPanel.LocationProperty.AddOwner(typeof(Pushpin));
#endif
public Pushpin()
{
@ -32,6 +37,18 @@ namespace MapControl
MapPanel.InitMapElement(this);
}
/// <summary>
/// Wrapper for the MapPanel.AutoCollapse attached property.
/// </summary>
public bool AutoCollapse
{
get { return (bool)GetValue(AutoCollapseProperty); }
set { SetValue(AutoCollapseProperty, value); }
}
/// <summary>
/// Wrapper for the MapPanel.Location attached property.
/// </summary>
public Location Location
{
get { return (Location)GetValue(LocationProperty); }