From 0d7387059b600e7130e113db13162ededfb56ba5 Mon Sep 17 00:00:00 2001 From: ClemensF Date: Mon, 11 Jan 2021 22:35:17 +0100 Subject: [PATCH] Added MapItem.Location and LocationMemberPath --- MapControl/Shared/MapItemsControl.cs | 32 ++++++++++++++++++ MapControl/Shared/MapPath.cs | 2 +- MapControl/Shared/Pushpin.cs | 16 +++++++-- MapControl/UWP/MapItemsControl.UWP.cs | 14 +++----- MapControl/UWP/MapPath.UWP.cs | 2 +- MapControl/WPF/MapItemsControl.WPF.cs | 12 ++----- MapControl/WPF/MapPath.WPF.cs | 2 +- SampleApps/UniversalApp/BindingHelper.cs | 36 --------------------- SampleApps/UniversalApp/MainPage.xaml | 4 +-- SampleApps/UniversalApp/UniversalApp.csproj | 1 - SampleApps/WpfApplication/MainWindow.xaml | 6 ++-- 11 files changed, 59 insertions(+), 68 deletions(-) delete mode 100644 SampleApps/UniversalApp/BindingHelper.cs diff --git a/MapControl/Shared/MapItemsControl.cs b/MapControl/Shared/MapItemsControl.cs index f43cd8e2..6903aef3 100644 --- a/MapControl/Shared/MapItemsControl.cs +++ b/MapControl/Shared/MapItemsControl.cs @@ -7,13 +7,45 @@ using System; using Windows.Foundation; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Data; #else using System.Windows; using System.Windows.Controls; +using System.Windows.Data; #endif namespace MapControl { + /// + /// Container class for an item in a MapItemsControl. + /// + public partial class MapItem : ListBoxItem + { + public static readonly DependencyProperty LocationMemberPathProperty = DependencyProperty.Register( + nameof(LocationMemberPath), typeof(string), typeof(MapItem), + new PropertyMetadata(null, (o, e) => BindingOperations.SetBinding( + o, LocationProperty, new Binding { Path = new PropertyPath((string)e.NewValue) }))); + + public MapItem() + { + DefaultStyleKey = typeof(MapItem); + + MapPanel.InitMapElement(this); + } + + public Location Location + { + get { return (Location)GetValue(LocationProperty); } + set { SetValue(LocationProperty, value); } + } + + public string LocationMemberPath + { + get { return (string)GetValue(LocationMemberPathProperty); } + set { SetValue(LocationMemberPathProperty, value); } + } + } + /// /// Manages a collection of selectable items on a Map. /// diff --git a/MapControl/Shared/MapPath.cs b/MapControl/Shared/MapPath.cs index 71328e5e..d98a8744 100644 --- a/MapControl/Shared/MapPath.cs +++ b/MapControl/Shared/MapPath.cs @@ -83,7 +83,7 @@ namespace MapControl } } - #region Method used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon + #region Methods used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon protected double GetLongitudeOffset(Location location) { diff --git a/MapControl/Shared/Pushpin.cs b/MapControl/Shared/Pushpin.cs index b5a1b3e9..a46d8401 100644 --- a/MapControl/Shared/Pushpin.cs +++ b/MapControl/Shared/Pushpin.cs @@ -3,18 +3,28 @@ // Licensed under the Microsoft Public License (Ms-PL) #if WINDOWS_UWP +using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; #else +using System.Windows; using System.Windows.Controls; #endif namespace MapControl { /// - /// Pushpin at a geographic location specified by the MapPanel.Location attached property. + /// Pushpin at a geographic location specified by the Location property. /// 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))); +#else + MapPanel.LocationProperty.AddOwner(typeof(Pushpin)); +#endif public Pushpin() { DefaultStyleKey = typeof(Pushpin); @@ -24,8 +34,8 @@ namespace MapControl public Location Location { - get { return MapPanel.GetLocation(this); } - set { MapPanel.SetLocation(this, value); } + get { return (Location)GetValue(LocationProperty); } + set { SetValue(LocationProperty, value); } } } } diff --git a/MapControl/UWP/MapItemsControl.UWP.cs b/MapControl/UWP/MapItemsControl.UWP.cs index 8254a4dd..941c7e99 100644 --- a/MapControl/UWP/MapItemsControl.UWP.cs +++ b/MapControl/UWP/MapItemsControl.UWP.cs @@ -9,17 +9,11 @@ using Windows.UI.Xaml.Input; namespace MapControl { - /// - /// Container class for an item in a MapItemsControl. - /// - public class MapItem : ListBoxItem + public partial class MapItem { - public MapItem() - { - DefaultStyleKey = typeof(MapItem); - - MapPanel.InitMapElement(this); - } + public static readonly DependencyProperty LocationProperty = DependencyProperty.Register( + nameof(Location), typeof(Location), typeof(MapItem), + new PropertyMetadata(null, (o, e) => MapPanel.SetLocation((FrameworkElement)o, (Location)e.NewValue))); protected override void OnPointerPressed(PointerRoutedEventArgs e) { diff --git a/MapControl/UWP/MapPath.UWP.cs b/MapControl/UWP/MapPath.UWP.cs index a4ef5722..2822eba7 100644 --- a/MapControl/UWP/MapPath.UWP.cs +++ b/MapControl/UWP/MapPath.UWP.cs @@ -14,7 +14,7 @@ namespace MapControl { public partial class MapPath : Path { - #region Method used only by derived classes MapPolyline and MapPolygon + #region Methods used only by derived classes MapPolyline and MapPolygon protected void DataCollectionPropertyChanged(DependencyPropertyChangedEventArgs e) { diff --git a/MapControl/WPF/MapItemsControl.WPF.cs b/MapControl/WPF/MapItemsControl.WPF.cs index d9d80af5..950bb891 100644 --- a/MapControl/WPF/MapItemsControl.WPF.cs +++ b/MapControl/WPF/MapItemsControl.WPF.cs @@ -9,17 +9,9 @@ using System.Windows.Media; namespace MapControl { - /// - /// Container class for an item in a MapItemsControl. - /// - public class MapItem : ListBoxItem + public partial class MapItem { - public MapItem() - { - DefaultStyleKey = typeof(MapItem); - - MapPanel.InitMapElement(this); - } + public static readonly DependencyProperty LocationProperty = MapPanel.LocationProperty.AddOwner(typeof(MapItem)); protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { diff --git a/MapControl/WPF/MapPath.WPF.cs b/MapControl/WPF/MapPath.WPF.cs index a2d8f3f6..7e79ad85 100644 --- a/MapControl/WPF/MapPath.WPF.cs +++ b/MapControl/WPF/MapPath.WPF.cs @@ -27,7 +27,7 @@ namespace MapControl get { return Data; } } - #region Method used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon + #region Methods used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon protected void DataCollectionPropertyChanged(DependencyPropertyChangedEventArgs e) { diff --git a/SampleApps/UniversalApp/BindingHelper.cs b/SampleApps/UniversalApp/BindingHelper.cs deleted file mode 100644 index abef67a9..00000000 --- a/SampleApps/UniversalApp/BindingHelper.cs +++ /dev/null @@ -1,36 +0,0 @@ -using MapControl; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Data; - -namespace UniversalApp -{ - public class BindingHelper - { - public static readonly DependencyProperty LocationPathProperty = DependencyProperty.RegisterAttached( - "LocationPath", typeof(string), typeof(BindingHelper), - new PropertyMetadata(null, LocationPathPropertyChanged)); - - public static string GetLocationPath(DependencyObject obj) - { - return (string)obj.GetValue(LocationPathProperty); - } - - public static void SetLocationPath(DependencyObject obj, string value) - { - obj.SetValue(LocationPathProperty, value); - } - - private static void LocationPathPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) - { - var propertyPath = e.NewValue as string; - - if (propertyPath != null) - { - BindingOperations.SetBinding( - obj, - MapPanel.LocationProperty, - new Binding { Path = new PropertyPath(propertyPath) }); - } - } - } -} diff --git a/SampleApps/UniversalApp/MainPage.xaml b/SampleApps/UniversalApp/MainPage.xaml index 3c72eb44..3828fc48 100644 --- a/SampleApps/UniversalApp/MainPage.xaml +++ b/SampleApps/UniversalApp/MainPage.xaml @@ -21,7 +21,7 @@