Updated BindingHelper

This commit is contained in:
Clemens 2021-12-01 20:15:48 +01:00
parent 0714a04a92
commit 928d12876f
7 changed files with 61 additions and 30 deletions

View file

@ -17,18 +17,34 @@ namespace MapControl
{
internal static class BindingHelper
{
public static Binding GetBinding(this object sourceObject, string sourceProperty)
/// <summary>
/// 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.
/// </summary>
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)
/// <summary>
/// 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).
/// </summary>
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));
}
}
}

View file

@ -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);