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

76 lines
2.7 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2022-01-14 20:22:56 +01:00
// © 2022 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
2021-06-14 21:41:37 +02:00
#if WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
#else
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
2021-06-14 21:41:37 +02:00
#endif
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 ViewPositionProperty = DependencyProperty.RegisterAttached(
"ViewPosition", typeof(Point?), typeof(MapPanel), new PropertyMetadata(null));
2021-01-17 00:31:30 +01:00
public MapPanel()
{
InitMapElement(this);
}
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
{
2021-06-14 21:41:37 +02:00
// Workaround for missing property value inheritance.
2017-09-05 20:57:17 +02:00
// Loaded and Unloaded handlers set and clear the ParentMap property value.
2022-08-02 19:50:11 +02:00
element.Loaded += (s, e) => GetParentMap((FrameworkElement)s);
element.Unloaded += (s, e) => ((FrameworkElement)s).ClearValue(ParentMapProperty);
2017-09-05 20:57:17 +02:00
}
}
public static MapBase GetParentMap(FrameworkElement element)
{
var parentMap = (MapBase)element.GetValue(ParentMapProperty);
2022-08-02 19:54:14 +02:00
if (parentMap == null &&
VisualTreeHelper.GetParent(element) is FrameworkElement parentElement)
{
2022-08-02 19:50:11 +02:00
parentMap = (parentElement as MapBase) ?? GetParentMap(parentElement);
if (parentMap != null)
{
element.SetValue(ParentMapProperty, parentMap);
}
}
return parentMap;
}
2022-11-05 17:32:29 +01:00
private static void SetViewPosition(FrameworkElement element, Point? position)
{
2022-11-05 17:32:29 +01:00
element.SetValue(ViewPositionProperty, position);
}
}
}