From 928d12876f659e7145036dc17aae455fdaf3ada5 Mon Sep 17 00:00:00 2001 From: Clemens Date: Wed, 1 Dec 2021 20:15:48 +0100 Subject: [PATCH] Updated BindingHelper --- MapControl/Shared/BindingHelper.cs | 30 ++++++++++++++++----- MapControl/Shared/MapScale.cs | 6 ++--- MapControl/WPF/MapOverlay.WPF.cs | 4 ++- MapControl/WinUI/MapContentControl.WinUI.cs | 11 +++++--- MapControl/WinUI/MapGraticule.WinUI.cs | 22 +++++++-------- MapControl/WinUI/MapItemsControl.WinUI.cs | 11 +++++--- MapControl/WinUI/MapOverlay.WinUI.cs | 7 +++-- 7 files changed, 61 insertions(+), 30 deletions(-) diff --git a/MapControl/Shared/BindingHelper.cs b/MapControl/Shared/BindingHelper.cs index ef2c7b49..71012732 100644 --- a/MapControl/Shared/BindingHelper.cs +++ b/MapControl/Shared/BindingHelper.cs @@ -17,18 +17,34 @@ namespace MapControl { internal static class BindingHelper { - public static Binding GetBinding(this object sourceObject, string sourceProperty) + /// + /// Returns a Binding to the specified dependency property of a FrameworkElement. + /// If the source property is itself already bound, the method returns the existing Binding, + /// otherwise it creates one with sourceElement as Source and sourcePropertyName as Path. + /// + public static Binding GetOrCreateBinding( + this FrameworkElement sourceElement, DependencyProperty sourceProperty, string sourcePropertyName) { - return new Binding { Source = sourceObject, Path = new PropertyPath(sourceProperty) }; + var sourceBinding = sourceElement.GetBindingExpression(sourceProperty); + + return sourceBinding != null + ? sourceBinding.ParentBinding + : new Binding { Source = sourceElement, Path = new PropertyPath(sourcePropertyName) }; } - public static void ValidateProperty( - this FrameworkElement targetObject, DependencyProperty targetProperty, object sourceObject, string sourceProperty) + /// + /// Sets a Binding on the specified dependency property of targetElement, if the target property does + /// not yet have a value or a Binding assigned to it. The potentially assigned Binding is created by + /// GetOrCreateBinding(sourceElement, sourceProperty, sourcePropertyName). + /// + public static void SetBindingOnUnsetProperty( + this FrameworkElement targetElement, DependencyProperty targetProperty, + FrameworkElement sourceElement, DependencyProperty sourceProperty, string sourcePropertyName) { - if (targetObject.GetValue(targetProperty) == null && - targetObject.GetBindingExpression(targetProperty) == null) + if (targetElement.GetValue(targetProperty) == null && + targetElement.GetBindingExpression(targetProperty) == null) { - targetObject.SetBinding(targetProperty, sourceObject.GetBinding(sourceProperty)); + targetElement.SetBinding(targetProperty, GetOrCreateBinding(sourceElement, sourceProperty, sourcePropertyName)); } } } diff --git a/MapControl/Shared/MapScale.cs b/MapControl/Shared/MapScale.cs index 2fe66aa4..989468c1 100644 --- a/MapControl/Shared/MapScale.cs +++ b/MapControl/Shared/MapScale.cs @@ -46,10 +46,10 @@ namespace MapControl { MinWidth = 100d; - line.SetBinding(Shape.StrokeProperty, this.GetBinding(nameof(Stroke))); - line.SetBinding(Shape.StrokeThicknessProperty, this.GetBinding(nameof(StrokeThickness))); + line.SetBinding(Shape.StrokeProperty, this.GetOrCreateBinding(StrokeProperty, nameof(Stroke))); + line.SetBinding(Shape.StrokeThicknessProperty, this.GetOrCreateBinding(StrokeThicknessProperty, nameof(StrokeThickness))); #if WINUI || UWP - label.SetBinding(TextBlock.ForegroundProperty, this.GetBinding(nameof(Foreground))); + label.SetBinding(TextBlock.ForegroundProperty, this.GetOrCreateBinding(ForegroundProperty, nameof(Foreground))); #endif Children.Add(line); Children.Add(label); diff --git a/MapControl/WPF/MapOverlay.WPF.cs b/MapControl/WPF/MapOverlay.WPF.cs index e599b436..00d06de0 100644 --- a/MapControl/WPF/MapOverlay.WPF.cs +++ b/MapControl/WPF/MapOverlay.WPF.cs @@ -65,7 +65,9 @@ namespace MapControl protected override void OnInitialized(EventArgs e) { base.OnInitialized(e); - this.ValidateProperty(StrokeProperty, this, nameof(Foreground)); + + // If this.Stroke is not explicitly set, bind it to this.Foreground + this.SetBindingOnUnsetProperty(StrokeProperty, this, ForegroundProperty, nameof(Foreground)); } public Pen CreatePen() diff --git a/MapControl/WinUI/MapContentControl.WinUI.cs b/MapControl/WinUI/MapContentControl.WinUI.cs index 6b921947..1af0b711 100644 --- a/MapControl/WinUI/MapContentControl.WinUI.cs +++ b/MapControl/WinUI/MapContentControl.WinUI.cs @@ -57,9 +57,14 @@ namespace MapControl if (parentMap != null) { - this.ValidateProperty(BackgroundProperty, parentMap, nameof(MapBase.Background)); - this.ValidateProperty(BorderBrushProperty, parentMap, nameof(MapBase.Foreground)); - this.ValidateProperty(ForegroundProperty, parentMap, nameof(MapBase.Foreground)); + // If this.Background is not explicitly set, bind it to parentMap.Background + this.SetBindingOnUnsetProperty(BackgroundProperty, parentMap, Panel.BackgroundProperty, nameof(Background)); + + // If this.Foreground is not explicitly set, bind it to parentMap.Foreground + this.SetBindingOnUnsetProperty(ForegroundProperty, parentMap, MapBase.ForegroundProperty, nameof(Foreground)); + + // If this.BorderBrush is not explicitly set, bind it to parentMap.Foreground + this.SetBindingOnUnsetProperty(BorderBrushProperty, parentMap, MapBase.ForegroundProperty, nameof(Foreground)); } } } diff --git a/MapControl/WinUI/MapGraticule.WinUI.cs b/MapControl/WinUI/MapGraticule.WinUI.cs index 2bb4fe82..2122af7d 100644 --- a/MapControl/WinUI/MapGraticule.WinUI.cs +++ b/MapControl/WinUI/MapGraticule.WinUI.cs @@ -35,11 +35,11 @@ namespace MapControl if (path == null) { path = new Path { Data = new PathGeometry() }; - 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))); + path.SetBinding(Shape.StrokeProperty, this.GetOrCreateBinding(StrokeProperty, nameof(Stroke))); + path.SetBinding(Shape.StrokeThicknessProperty, this.GetOrCreateBinding(StrokeThicknessProperty, nameof(StrokeThickness))); + path.SetBinding(Shape.StrokeDashArrayProperty, this.GetOrCreateBinding(StrokeDashArrayProperty, nameof(StrokeDashArray))); + path.SetBinding(Shape.StrokeDashOffsetProperty, this.GetOrCreateBinding(StrokeDashOffsetProperty, nameof(StrokeDashOffset))); + path.SetBinding(Shape.StrokeDashCapProperty, this.GetOrCreateBinding(StrokeDashCapProperty, nameof(StrokeDashCap))); Children.Add(path); } @@ -115,15 +115,15 @@ namespace MapControl else { label = new TextBlock { RenderTransform = new MatrixTransform() }; - 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))); + label.SetBinding(TextBlock.FontSizeProperty, this.GetOrCreateBinding(FontSizeProperty, nameof(FontSize))); + label.SetBinding(TextBlock.FontStyleProperty, this.GetOrCreateBinding(FontStyleProperty, nameof(FontStyle))); + label.SetBinding(TextBlock.FontStretchProperty, this.GetOrCreateBinding(FontStretchProperty, nameof(FontStretch))); + label.SetBinding(TextBlock.FontWeightProperty, this.GetOrCreateBinding(FontWeightProperty, nameof(FontWeight))); + label.SetBinding(TextBlock.ForegroundProperty, this.GetOrCreateBinding(ForegroundProperty, nameof(Foreground))); if (FontFamily != null) { - label.SetBinding(TextBlock.FontFamilyProperty, this.GetBinding(nameof(FontFamily))); + label.SetBinding(TextBlock.FontFamilyProperty, this.GetOrCreateBinding(FontFamilyProperty, nameof(FontFamily))); } Children.Add(label); diff --git a/MapControl/WinUI/MapItemsControl.WinUI.cs b/MapControl/WinUI/MapItemsControl.WinUI.cs index ef07a625..24bf158a 100644 --- a/MapControl/WinUI/MapItemsControl.WinUI.cs +++ b/MapControl/WinUI/MapItemsControl.WinUI.cs @@ -45,9 +45,14 @@ namespace MapControl if (parentMap != null) { - this.ValidateProperty(BackgroundProperty, parentMap, nameof(MapBase.Background)); - this.ValidateProperty(BorderBrushProperty, parentMap, nameof(MapBase.Foreground)); - this.ValidateProperty(ForegroundProperty, parentMap, nameof(MapBase.Foreground)); + // If this.Background is not explicitly set, bind it to parentMap.Background + this.SetBindingOnUnsetProperty(BackgroundProperty, parentMap, Panel.BackgroundProperty, nameof(Background)); + + // If this.Foreground is not explicitly set, bind it to parentMap.Foreground + this.SetBindingOnUnsetProperty(ForegroundProperty, parentMap, MapBase.ForegroundProperty, nameof(Foreground)); + + // If this.BorderBrush is not explicitly set, bind it to parentMap.Foreground + this.SetBindingOnUnsetProperty(BorderBrushProperty, parentMap, MapBase.ForegroundProperty, nameof(Foreground)); } } } diff --git a/MapControl/WinUI/MapOverlay.WinUI.cs b/MapControl/WinUI/MapOverlay.WinUI.cs index d66b0b81..5c38963e 100644 --- a/MapControl/WinUI/MapOverlay.WinUI.cs +++ b/MapControl/WinUI/MapOverlay.WinUI.cs @@ -71,8 +71,11 @@ namespace MapControl { if (map != null) { - this.ValidateProperty(ForegroundProperty, map, nameof(MapBase.Foreground)); - this.ValidateProperty(StrokeProperty, this, nameof(Foreground)); + // If this.Forground is not explicitly set, bind it to map.Foreground + this.SetBindingOnUnsetProperty(ForegroundProperty, map, MapBase.ForegroundProperty, nameof(Foreground)); + + // If this.Stroke is not explicitly set, bind it to this.Foreground + this.SetBindingOnUnsetProperty(StrokeProperty, this, ForegroundProperty, nameof(Foreground)); } base.SetParentMap(map);