XAML-Map-Control/MapControl/UWP/MapPanel.UWP.cs

70 lines
2.7 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2019 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
namespace MapControl
{
public partial class MapPanel
{
public static readonly DependencyProperty LocationProperty = DependencyProperty.RegisterAttached(
"Location", typeof(Location), typeof(MapPanel),
new PropertyMetadata(null, (o, e) => (((FrameworkElement)o).Parent as MapPanel)?.InvalidateArrange()));
public static readonly DependencyProperty BoundingBoxProperty = DependencyProperty.RegisterAttached(
"BoundingBox", typeof(BoundingBox), typeof(MapPanel),
new PropertyMetadata(null, (o, e) => (((FrameworkElement)o).Parent as MapPanel)?.InvalidateArrange()));
public static readonly DependencyProperty ParentMapProperty = DependencyProperty.RegisterAttached(
"ParentMap", typeof(MapBase), typeof(MapPanel), new PropertyMetadata(null, ParentMapPropertyChanged));
private static readonly DependencyProperty ViewportPositionProperty = DependencyProperty.RegisterAttached(
"ViewportPosition", typeof(Point?), typeof(MapPanel), new PropertyMetadata(null));
2017-09-05 20:57:17 +02:00
public static void InitMapElement(FrameworkElement element)
{
2017-09-05 20:57:17 +02:00
if (element is MapBase)
{
2017-09-05 20:57:17 +02:00
element.SetValue(ParentMapProperty, element);
}
else
{
2017-09-05 20:57:17 +02:00
// Workaround for missing property value inheritance in Windows Runtime.
// Loaded and Unloaded handlers set and clear the ParentMap property value.
2017-09-05 20:57:17 +02:00
element.Loaded += (s, e) => GetParentMap(element);
element.Unloaded += (s, e) => element.ClearValue(ParentMapProperty);
}
}
public static MapBase GetParentMap(FrameworkElement element)
{
var parentMap = (MapBase)element.GetValue(ParentMapProperty);
if (parentMap == null && (parentMap = FindParentMap(element)) != null)
{
element.SetValue(ParentMapProperty, parentMap);
}
return parentMap;
}
private static MapBase FindParentMap(FrameworkElement element)
{
var parent = VisualTreeHelper.GetParent(element) as FrameworkElement;
2017-09-05 20:57:17 +02:00
return parent == null ? null
: ((parent as MapBase)
?? (MapBase)element.GetValue(ParentMapProperty)
?? FindParentMap(parent));
}
private static void SetViewportPosition(FrameworkElement element, Point? viewportPosition)
{
element.SetValue(ViewportPositionProperty, viewportPosition);
}
}
}