From 068a9ef8a6bb3b3a29a30eafa617a6e306efc96b Mon Sep 17 00:00:00 2001 From: ClemensF Date: Sun, 17 Jan 2021 23:39:20 +0100 Subject: [PATCH] Added PropertyHelper --- MapControl/Shared/MapOverlay.cs | 8 ------- MapControl/Shared/MapScale.cs | 6 ++--- MapControl/Shared/PropertyHelper.cs | 32 +++++++++++++++++++++++++ MapControl/UWP/MapContentControl.UWP.cs | 22 ++++------------- MapControl/UWP/MapControl.UWP.csproj | 3 +++ MapControl/UWP/MapGraticule.UWP.cs | 22 ++++++++--------- MapControl/UWP/MapItemsControl.UWP.cs | 14 +++++++++++ MapControl/UWP/MapOverlay.UWP.cs | 12 ++-------- MapControl/UWP/Themes/Generic.xaml | 11 ++++----- MapControl/WPF/MapOverlay.WPF.cs | 6 +---- SampleApps/UniversalApp/MainPage.xaml | 1 - 11 files changed, 76 insertions(+), 61 deletions(-) create mode 100644 MapControl/Shared/PropertyHelper.cs diff --git a/MapControl/Shared/MapOverlay.cs b/MapControl/Shared/MapOverlay.cs index 339c621e..345343f7 100644 --- a/MapControl/Shared/MapOverlay.cs +++ b/MapControl/Shared/MapOverlay.cs @@ -4,12 +4,9 @@ #if WINDOWS_UWP using Windows.UI.Text; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Media; #else using System.Windows; -using System.Windows.Data; using System.Windows.Media; #endif @@ -109,10 +106,5 @@ namespace MapControl get { return (double)GetValue(StrokeMiterLimitProperty); } set { SetValue(StrokeMiterLimitProperty, value); } } - - protected Binding GetBinding(string propertyName) - { - return new Binding { Source = this, Path = new PropertyPath(propertyName) }; - } } } diff --git a/MapControl/Shared/MapScale.cs b/MapControl/Shared/MapScale.cs index 11805383..54b5d4c5 100644 --- a/MapControl/Shared/MapScale.cs +++ b/MapControl/Shared/MapScale.cs @@ -39,10 +39,10 @@ namespace MapControl { MinWidth = 100d; - line.SetBinding(Shape.StrokeProperty, GetBinding(nameof(Stroke))); - line.SetBinding(Shape.StrokeThicknessProperty, GetBinding(nameof(StrokeThickness))); + line.SetBinding(Shape.StrokeProperty, this.GetBinding(nameof(Stroke))); + line.SetBinding(Shape.StrokeThicknessProperty, this.GetBinding(nameof(StrokeThickness))); #if WINDOWS_UWP - label.SetBinding(TextBlock.ForegroundProperty, GetBinding(nameof(Foreground))); + label.SetBinding(TextBlock.ForegroundProperty, this.GetBinding(nameof(Foreground))); #endif Children.Add(line); Children.Add(label); diff --git a/MapControl/Shared/PropertyHelper.cs b/MapControl/Shared/PropertyHelper.cs new file mode 100644 index 00000000..d3bab6f0 --- /dev/null +++ b/MapControl/Shared/PropertyHelper.cs @@ -0,0 +1,32 @@ +// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control +// © 2021 Clemens Fischer +// Licensed under the Microsoft Public License (Ms-PL) + +#if WINDOWS_UWP +using Windows.UI.Xaml; +using Windows.UI.Xaml.Data; +#else +using System.Windows; +using System.Windows.Data; +#endif + +namespace MapControl +{ + public static class PropertyHelper + { + public static Binding GetBinding(this object sourceObject, string sourceProperty) + { + return new Binding { Source = sourceObject, Path = new PropertyPath(sourceProperty) }; + } + + public static void ValidateProperty( + this FrameworkElement targetObject, DependencyProperty targetProperty, object sourceObject, string sourceProperty) + { + if (targetObject.GetValue(targetProperty) == null && + targetObject.GetBindingExpression(targetProperty) == null) + { + targetObject.SetBinding(targetProperty, sourceObject.GetBinding(sourceProperty)); + } + } + } +} diff --git a/MapControl/UWP/MapContentControl.UWP.cs b/MapControl/UWP/MapContentControl.UWP.cs index ac6469a1..db26c3c1 100644 --- a/MapControl/UWP/MapContentControl.UWP.cs +++ b/MapControl/UWP/MapContentControl.UWP.cs @@ -4,7 +4,6 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Data; namespace MapControl { @@ -49,24 +48,13 @@ namespace MapControl { base.OnApplyTemplate(); - var map = MapPanel.GetParentMap(this); + var parentMap = MapPanel.GetParentMap(this); - if (map != null) + if (parentMap != null) { - if (Background == null) - { - SetBinding(BackgroundProperty, new Binding { Source = map, Path = new PropertyPath(nameof(MapBase.Background)) }); - } - - if (BorderBrush == null) - { - SetBinding(BorderBrushProperty, new Binding { Source = map, Path = new PropertyPath(nameof(MapBase.Foreground)) }); - } - - if (Foreground == null) - { - SetBinding(ForegroundProperty, new Binding { Source = map, Path = new PropertyPath(nameof(MapBase.Foreground)) }); - } + this.ValidateProperty(BackgroundProperty, parentMap, nameof(MapBase.Background)); + this.ValidateProperty(BorderBrushProperty, parentMap, nameof(MapBase.Foreground)); + this.ValidateProperty(ForegroundProperty, parentMap, nameof(MapBase.Foreground)); } } } diff --git a/MapControl/UWP/MapControl.UWP.csproj b/MapControl/UWP/MapControl.UWP.csproj index 6897fd43..737eeabb 100644 --- a/MapControl/UWP/MapControl.UWP.csproj +++ b/MapControl/UWP/MapControl.UWP.csproj @@ -131,6 +131,9 @@ OrthographicProjection.cs + + PropertyHelper.cs + StereographicProjection.cs diff --git a/MapControl/UWP/MapGraticule.UWP.cs b/MapControl/UWP/MapGraticule.UWP.cs index 90d0919f..67fad11e 100644 --- a/MapControl/UWP/MapGraticule.UWP.cs +++ b/MapControl/UWP/MapGraticule.UWP.cs @@ -29,11 +29,11 @@ namespace MapControl if (path == null) { path = new Path { Data = new PathGeometry() }; - path.SetBinding(Shape.StrokeProperty, GetBinding(nameof(Stroke))); - path.SetBinding(Shape.StrokeThicknessProperty, GetBinding(nameof(StrokeThickness))); - path.SetBinding(Shape.StrokeDashArrayProperty, GetBinding(nameof(StrokeDashArray))); - path.SetBinding(Shape.StrokeDashOffsetProperty, GetBinding(nameof(StrokeDashOffset))); - path.SetBinding(Shape.StrokeDashCapProperty, GetBinding(nameof(StrokeDashCap))); + path.SetBinding(Shape.StrokeProperty, this.GetBinding(nameof(Stroke))); + path.SetBinding(Shape.StrokeThicknessProperty, this.GetBinding(nameof(StrokeThickness))); + path.SetBinding(Shape.StrokeDashArrayProperty, this.GetBinding(nameof(StrokeDashArray))); + path.SetBinding(Shape.StrokeDashOffsetProperty, this.GetBinding(nameof(StrokeDashOffset))); + path.SetBinding(Shape.StrokeDashCapProperty, this.GetBinding(nameof(StrokeDashCap))); Children.Add(path); } @@ -109,15 +109,15 @@ namespace MapControl else { label = new TextBlock { RenderTransform = new MatrixTransform() }; - label.SetBinding(TextBlock.FontSizeProperty, GetBinding(nameof(FontSize))); - label.SetBinding(TextBlock.FontStyleProperty, GetBinding(nameof(FontStyle))); - label.SetBinding(TextBlock.FontStretchProperty, GetBinding(nameof(FontStretch))); - label.SetBinding(TextBlock.FontWeightProperty, GetBinding(nameof(FontWeight))); - label.SetBinding(TextBlock.ForegroundProperty, GetBinding(nameof(Foreground))); + label.SetBinding(TextBlock.FontSizeProperty, this.GetBinding(nameof(FontSize))); + label.SetBinding(TextBlock.FontStyleProperty, this.GetBinding(nameof(FontStyle))); + label.SetBinding(TextBlock.FontStretchProperty, this.GetBinding(nameof(FontStretch))); + label.SetBinding(TextBlock.FontWeightProperty, this.GetBinding(nameof(FontWeight))); + label.SetBinding(TextBlock.ForegroundProperty, this.GetBinding(nameof(Foreground))); if (FontFamily != null) { - label.SetBinding(TextBlock.FontFamilyProperty, GetBinding(nameof(FontFamily))); + label.SetBinding(TextBlock.FontFamilyProperty, this.GetBinding(nameof(FontFamily))); } Children.Add(label); diff --git a/MapControl/UWP/MapItemsControl.UWP.cs b/MapControl/UWP/MapItemsControl.UWP.cs index 5837cb39..6a045233 100644 --- a/MapControl/UWP/MapItemsControl.UWP.cs +++ b/MapControl/UWP/MapItemsControl.UWP.cs @@ -30,6 +30,20 @@ namespace MapControl (ItemsControl.ItemsControlFromItemContainer(this) as MapItemsControl)?.OnItemClicked( this, e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control), e.KeyModifiers.HasFlag(VirtualKeyModifiers.Shift)); } + + protected override void OnApplyTemplate() + { + base.OnApplyTemplate(); + + var parentMap = MapPanel.GetParentMap(this); + + if (parentMap != null) + { + this.ValidateProperty(BackgroundProperty, parentMap, nameof(MapBase.Background)); + this.ValidateProperty(BorderBrushProperty, parentMap, nameof(MapBase.Foreground)); + this.ValidateProperty(ForegroundProperty, parentMap, nameof(MapBase.Foreground)); + } + } } public partial class MapItemsControl diff --git a/MapControl/UWP/MapOverlay.UWP.cs b/MapControl/UWP/MapOverlay.UWP.cs index beaa976a..81b8daf9 100644 --- a/MapControl/UWP/MapOverlay.UWP.cs +++ b/MapControl/UWP/MapOverlay.UWP.cs @@ -4,7 +4,6 @@ using Windows.UI.Text; using Windows.UI.Xaml; -using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Media; namespace MapControl @@ -65,15 +64,8 @@ namespace MapControl { if (map != null) { - if (Foreground == null) - { - SetBinding(ForegroundProperty, new Binding { Source = map, Path = new PropertyPath(nameof(MapBase.Foreground)) }); - } - - if (Stroke == null) - { - SetBinding(StrokeProperty, GetBinding(nameof(Foreground))); - } + this.ValidateProperty(ForegroundProperty, map, nameof(MapBase.Foreground)); + this.ValidateProperty(StrokeProperty, this, nameof(Foreground)); } base.SetParentMap(map); diff --git a/MapControl/UWP/Themes/Generic.xaml b/MapControl/UWP/Themes/Generic.xaml index de66ed1b..e2a4eadc 100644 --- a/MapControl/UWP/Themes/Generic.xaml +++ b/MapControl/UWP/Themes/Generic.xaml @@ -49,7 +49,6 @@