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

View file

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

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

View file

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

View file

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

View file

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

View file

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

View file

@ -49,7 +49,6 @@
<Style TargetType="map:Pushpin" BasedOn="{StaticResource ContentControlStyle}">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="UseLayoutRounding" Value="True"/>
<Setter Property="Padding" Value="5,3"/>
<Setter Property="Template">
<Setter.Value>
@ -63,11 +62,11 @@
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1">
<ContentPresenter Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</Border>
<Path Grid.Row="1"
Data="M0.5,-1 L0.5,15 10,-1"

View file

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

View file

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