Added PropertyHelper

This commit is contained in:
ClemensF 2021-01-17 23:39:20 +01:00
parent 67e9989327
commit 068a9ef8a6
11 changed files with 76 additions and 61 deletions

View file

@ -4,12 +4,9 @@
#if WINDOWS_UWP #if WINDOWS_UWP
using Windows.UI.Text; using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media;
#else #else
using System.Windows; using System.Windows;
using System.Windows.Data;
using System.Windows.Media; using System.Windows.Media;
#endif #endif
@ -109,10 +106,5 @@ namespace MapControl
get { return (double)GetValue(StrokeMiterLimitProperty); } get { return (double)GetValue(StrokeMiterLimitProperty); }
set { SetValue(StrokeMiterLimitProperty, value); } set { SetValue(StrokeMiterLimitProperty, value); }
} }
protected Binding GetBinding(string propertyName)
{
return new Binding { Source = this, Path = new PropertyPath(propertyName) };
}
} }
} }

View file

@ -39,10 +39,10 @@ namespace MapControl
{ {
MinWidth = 100d; MinWidth = 100d;
line.SetBinding(Shape.StrokeProperty, GetBinding(nameof(Stroke))); line.SetBinding(Shape.StrokeProperty, this.GetBinding(nameof(Stroke)));
line.SetBinding(Shape.StrokeThicknessProperty, GetBinding(nameof(StrokeThickness))); line.SetBinding(Shape.StrokeThicknessProperty, this.GetBinding(nameof(StrokeThickness)));
#if WINDOWS_UWP #if WINDOWS_UWP
label.SetBinding(TextBlock.ForegroundProperty, GetBinding(nameof(Foreground))); label.SetBinding(TextBlock.ForegroundProperty, this.GetBinding(nameof(Foreground)));
#endif #endif
Children.Add(line); Children.Add(line);
Children.Add(label); Children.Add(label);

View file

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

View file

@ -4,7 +4,6 @@
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
namespace MapControl namespace MapControl
{ {
@ -49,24 +48,13 @@ namespace MapControl
{ {
base.OnApplyTemplate(); base.OnApplyTemplate();
var map = MapPanel.GetParentMap(this); var parentMap = MapPanel.GetParentMap(this);
if (map != null) if (parentMap != null)
{ {
if (Background == null) this.ValidateProperty(BackgroundProperty, parentMap, nameof(MapBase.Background));
{ this.ValidateProperty(BorderBrushProperty, parentMap, nameof(MapBase.Foreground));
SetBinding(BackgroundProperty, new Binding { Source = map, Path = new PropertyPath(nameof(MapBase.Background)) }); this.ValidateProperty(ForegroundProperty, parentMap, nameof(MapBase.Foreground));
}
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)) });
}
} }
} }
} }

View file

@ -131,6 +131,9 @@
<Compile Include="..\Shared\OrthographicProjection.cs"> <Compile Include="..\Shared\OrthographicProjection.cs">
<Link>OrthographicProjection.cs</Link> <Link>OrthographicProjection.cs</Link>
</Compile> </Compile>
<Compile Include="..\Shared\PropertyHelper.cs">
<Link>PropertyHelper.cs</Link>
</Compile>
<Compile Include="..\Shared\StereographicProjection.cs"> <Compile Include="..\Shared\StereographicProjection.cs">
<Link>StereographicProjection.cs</Link> <Link>StereographicProjection.cs</Link>
</Compile> </Compile>

View file

@ -29,11 +29,11 @@ namespace MapControl
if (path == null) if (path == null)
{ {
path = new Path { Data = new PathGeometry() }; path = new Path { Data = new PathGeometry() };
path.SetBinding(Shape.StrokeProperty, GetBinding(nameof(Stroke))); path.SetBinding(Shape.StrokeProperty, this.GetBinding(nameof(Stroke)));
path.SetBinding(Shape.StrokeThicknessProperty, GetBinding(nameof(StrokeThickness))); path.SetBinding(Shape.StrokeThicknessProperty, this.GetBinding(nameof(StrokeThickness)));
path.SetBinding(Shape.StrokeDashArrayProperty, GetBinding(nameof(StrokeDashArray))); path.SetBinding(Shape.StrokeDashArrayProperty, this.GetBinding(nameof(StrokeDashArray)));
path.SetBinding(Shape.StrokeDashOffsetProperty, GetBinding(nameof(StrokeDashOffset))); path.SetBinding(Shape.StrokeDashOffsetProperty, this.GetBinding(nameof(StrokeDashOffset)));
path.SetBinding(Shape.StrokeDashCapProperty, GetBinding(nameof(StrokeDashCap))); path.SetBinding(Shape.StrokeDashCapProperty, this.GetBinding(nameof(StrokeDashCap)));
Children.Add(path); Children.Add(path);
} }
@ -109,15 +109,15 @@ namespace MapControl
else else
{ {
label = new TextBlock { RenderTransform = new MatrixTransform() }; label = new TextBlock { RenderTransform = new MatrixTransform() };
label.SetBinding(TextBlock.FontSizeProperty, GetBinding(nameof(FontSize))); label.SetBinding(TextBlock.FontSizeProperty, this.GetBinding(nameof(FontSize)));
label.SetBinding(TextBlock.FontStyleProperty, GetBinding(nameof(FontStyle))); label.SetBinding(TextBlock.FontStyleProperty, this.GetBinding(nameof(FontStyle)));
label.SetBinding(TextBlock.FontStretchProperty, GetBinding(nameof(FontStretch))); label.SetBinding(TextBlock.FontStretchProperty, this.GetBinding(nameof(FontStretch)));
label.SetBinding(TextBlock.FontWeightProperty, GetBinding(nameof(FontWeight))); label.SetBinding(TextBlock.FontWeightProperty, this.GetBinding(nameof(FontWeight)));
label.SetBinding(TextBlock.ForegroundProperty, GetBinding(nameof(Foreground))); label.SetBinding(TextBlock.ForegroundProperty, this.GetBinding(nameof(Foreground)));
if (FontFamily != null) if (FontFamily != null)
{ {
label.SetBinding(TextBlock.FontFamilyProperty, GetBinding(nameof(FontFamily))); label.SetBinding(TextBlock.FontFamilyProperty, this.GetBinding(nameof(FontFamily)));
} }
Children.Add(label); Children.Add(label);

View file

@ -30,6 +30,20 @@ namespace MapControl
(ItemsControl.ItemsControlFromItemContainer(this) as MapItemsControl)?.OnItemClicked( (ItemsControl.ItemsControlFromItemContainer(this) as MapItemsControl)?.OnItemClicked(
this, e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control), e.KeyModifiers.HasFlag(VirtualKeyModifiers.Shift)); 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 public partial class MapItemsControl

View file

@ -4,7 +4,6 @@
using Windows.UI.Text; using Windows.UI.Text;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media;
namespace MapControl namespace MapControl
@ -65,15 +64,8 @@ namespace MapControl
{ {
if (map != null) if (map != null)
{ {
if (Foreground == null) this.ValidateProperty(ForegroundProperty, map, nameof(MapBase.Foreground));
{ this.ValidateProperty(StrokeProperty, this, nameof(Foreground));
SetBinding(ForegroundProperty, new Binding { Source = map, Path = new PropertyPath(nameof(MapBase.Foreground)) });
}
if (Stroke == null)
{
SetBinding(StrokeProperty, GetBinding(nameof(Foreground)));
}
} }
base.SetParentMap(map); base.SetParentMap(map);

View file

@ -49,7 +49,6 @@
<Style TargetType="map:Pushpin" BasedOn="{StaticResource ContentControlStyle}"> <Style TargetType="map:Pushpin" BasedOn="{StaticResource ContentControlStyle}">
<Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Bottom"/> <Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="UseLayoutRounding" Value="True"/>
<Setter Property="Padding" Value="5,3"/> <Setter Property="Padding" Value="5,3"/>
<Setter Property="Template"> <Setter Property="Template">
<Setter.Value> <Setter.Value>

View file

@ -65,11 +65,7 @@ namespace MapControl
protected override void OnInitialized(EventArgs e) protected override void OnInitialized(EventArgs e)
{ {
base.OnInitialized(e); base.OnInitialized(e);
this.ValidateProperty(StrokeProperty, this, nameof(Foreground));
if (Stroke == null)
{
SetBinding(StrokeProperty, GetBinding(nameof(Foreground)));
}
} }
public Pen CreatePen() public Pen CreatePen()

View file

@ -23,7 +23,6 @@
<Style x:Key="PointItemStyle" TargetType="map:MapItem"> <Style x:Key="PointItemStyle" TargetType="map:MapItem">
<Setter Property="AutoCollapse" Value="True"/> <Setter Property="AutoCollapse" Value="True"/>
<Setter Property="LocationMemberPath" Value="Location"/> <Setter Property="LocationMemberPath" Value="Location"/>
<Setter Property="Foreground" Value="Gray"/>
<Setter Property="FontSize" Value="12"/> <Setter Property="FontSize" Value="12"/>
<Setter Property="Template"> <Setter Property="Template">
<Setter.Value> <Setter.Value>