diff --git a/MapControl/Avalonia/DependencyPropertyHelper.Avalonia.cs b/MapControl/Avalonia/DependencyPropertyHelper.Avalonia.cs index be6e38d9..626b3756 100644 --- a/MapControl/Avalonia/DependencyPropertyHelper.Avalonia.cs +++ b/MapControl/Avalonia/DependencyPropertyHelper.Avalonia.cs @@ -1,6 +1,6 @@ using System; -#pragma warning disable AVP1001 // The same AvaloniaProperty should not be registered twice +#pragma warning disable AVP1001 namespace MapControl { @@ -52,27 +52,6 @@ namespace MapControl return property; } - public static StyledProperty AddOwner( - StyledProperty property, - TValue defaultValue = default, - Action changed = null) - where TOwner : AvaloniaObject - { - var newProperty = property.AddOwner(); - - if (!Equals(defaultValue, property.GetMetadata(typeof(TOwner)).DefaultValue)) - { - newProperty.OverrideMetadata(new StyledPropertyMetadata(defaultValue)); - } - - if (changed != null) - { - newProperty.Changed.AddClassHandler((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value)); - } - - return newProperty; - } - public static void SetBinding(this AvaloniaObject target, AvaloniaProperty property, Binding binding) { target.Bind(property, binding); diff --git a/MapControl/Avalonia/MapBase.Avalonia.cs b/MapControl/Avalonia/MapBase.Avalonia.cs index 041a1551..da3e12c9 100644 --- a/MapControl/Avalonia/MapBase.Avalonia.cs +++ b/MapControl/Avalonia/MapBase.Avalonia.cs @@ -31,7 +31,7 @@ namespace MapControl public partial class MapBase { public static readonly StyledProperty ForegroundProperty = - DependencyPropertyHelper.AddOwner(TextElement.ForegroundProperty, Brushes.Black); + TextElement.ForegroundProperty.AddOwner(new StyledPropertyMetadata(new Optional(Brushes.Black))); public static readonly StyledProperty AnimationEasingProperty = DependencyPropertyHelper.Register(nameof(AnimationEasing), new QuadraticEaseOut()); diff --git a/MapControl/Avalonia/MapContentControl.Avalonia.cs b/MapControl/Avalonia/MapContentControl.Avalonia.cs index 7308b657..766c75c8 100644 --- a/MapControl/Avalonia/MapContentControl.Avalonia.cs +++ b/MapControl/Avalonia/MapContentControl.Avalonia.cs @@ -6,10 +6,10 @@ public class MapContentControl : ContentControl { public static readonly StyledProperty AutoCollapseProperty = - DependencyPropertyHelper.AddOwner(MapPanel.AutoCollapseProperty); + MapPanel.AutoCollapseProperty.AddOwner(); public static readonly StyledProperty LocationProperty = - DependencyPropertyHelper.AddOwner(MapPanel.LocationProperty); + MapPanel.LocationProperty.AddOwner(); /// /// Gets/sets MapPanel.AutoCollapse. diff --git a/MapControl/Avalonia/MapGraticule.Avalonia.cs b/MapControl/Avalonia/MapGraticule.Avalonia.cs index f65f9efe..35be150d 100644 --- a/MapControl/Avalonia/MapGraticule.Avalonia.cs +++ b/MapControl/Avalonia/MapGraticule.Avalonia.cs @@ -12,31 +12,13 @@ namespace MapControl } public static readonly StyledProperty ForegroundProperty = - DependencyPropertyHelper.AddOwner(TextElement.ForegroundProperty); + TextElement.ForegroundProperty.AddOwner(); public static readonly StyledProperty FontFamilyProperty = - DependencyPropertyHelper.AddOwner(TextElement.FontFamilyProperty); + TextElement.FontFamilyProperty.AddOwner(); public static readonly StyledProperty FontSizeProperty = - DependencyPropertyHelper.AddOwner(TextElement.FontSizeProperty, 12d); - - public Brush Foreground - { - get => GetValue(ForegroundProperty); - set => SetValue(ForegroundProperty, value); - } - - public FontFamily FontFamily - { - get => GetValue(FontFamilyProperty); - set => SetValue(FontFamilyProperty, value); - } - - public double FontSize - { - get => GetValue(FontSizeProperty); - set => SetValue(FontSizeProperty, value); - } + TextElement.FontSizeProperty.AddOwner(new StyledPropertyMetadata(12d)); private MapBase parentMap; diff --git a/MapControl/Avalonia/MapItem.Avalonia.cs b/MapControl/Avalonia/MapItem.Avalonia.cs index 93727d90..3c35625e 100644 --- a/MapControl/Avalonia/MapItem.Avalonia.cs +++ b/MapControl/Avalonia/MapItem.Avalonia.cs @@ -3,11 +3,15 @@ public partial class MapItem { public static readonly StyledProperty AutoCollapseProperty = - DependencyPropertyHelper.AddOwner(MapPanel.AutoCollapseProperty); + MapPanel.AutoCollapseProperty.AddOwner(); public static readonly StyledProperty LocationProperty = - DependencyPropertyHelper.AddOwner(MapPanel.LocationProperty, null, - (item, oldValue, newValue) => item.UpdateMapTransform()); + MapPanel.LocationProperty.AddOwner(); + + static MapItem() + { + LocationProperty.Changed.AddClassHandler((item, e) => item.UpdateMapTransform()); + } protected override void OnPointerPressed(PointerPressedEventArgs e) { diff --git a/MapControl/Avalonia/MapPath.Avalonia.cs b/MapControl/Avalonia/MapPath.Avalonia.cs index 5977ed99..63265b33 100644 --- a/MapControl/Avalonia/MapPath.Avalonia.cs +++ b/MapControl/Avalonia/MapPath.Avalonia.cs @@ -4,14 +4,12 @@ namespace MapControl { public partial class MapPath : Shape { - public MapPath() - { - Stretch = Stretch.None; - } + public static readonly StyledProperty DataProperty = Path.DataProperty.AddOwner(); - public static readonly StyledProperty DataProperty = - DependencyPropertyHelper.AddOwner(Path.DataProperty, null, - (path, oldValue, newValue) => path.UpdateData()); + static MapPath() + { + DataProperty.Changed.AddClassHandler((path, e) => path.UpdateData()); + } public Geometry Data { diff --git a/MapControl/Shared/MapGraticule.cs b/MapControl/Shared/MapGraticule.cs index 12c21a89..6f4bcb13 100644 --- a/MapControl/Shared/MapGraticule.cs +++ b/MapControl/Shared/MapGraticule.cs @@ -40,17 +40,11 @@ namespace MapControl private const double LineInterpolationResolution = 2d; - public static readonly DependencyProperty StrokeThicknessProperty = - DependencyPropertyHelper.Register(nameof(StrokeThickness), 0.5); - public static readonly DependencyProperty MinLineDistanceProperty = DependencyPropertyHelper.Register(nameof(MinLineDistance), 150d); - public double StrokeThickness - { - get => (double)GetValue(StrokeThicknessProperty); - set => SetValue(StrokeThicknessProperty, value); - } + public static readonly DependencyProperty StrokeThicknessProperty = + DependencyPropertyHelper.Register(nameof(StrokeThickness), 0.5); /// /// Minimum graticule line distance in pixels. The default value is 150. @@ -61,6 +55,30 @@ namespace MapControl set => SetValue(MinLineDistanceProperty, value); } + public double StrokeThickness + { + get => (double)GetValue(StrokeThicknessProperty); + set => SetValue(StrokeThicknessProperty, value); + } + + public Brush Foreground + { + get => (Brush)GetValue(ForegroundProperty); + set => SetValue(ForegroundProperty, value); + } + + public FontFamily FontFamily + { + get => (FontFamily)GetValue(FontFamilyProperty); + set => SetValue(FontFamilyProperty, value); + } + + public double FontSize + { + get => (double)GetValue(FontSizeProperty); + set => SetValue(FontSizeProperty, value); + } + private double lineDistance; private string labelFormat; diff --git a/MapControl/WPF/DependencyPropertyHelper.WPF.cs b/MapControl/WPF/DependencyPropertyHelper.WPF.cs index b5cfc2ec..73fbaa9d 100644 --- a/MapControl/WPF/DependencyPropertyHelper.WPF.cs +++ b/MapControl/WPF/DependencyPropertyHelper.WPF.cs @@ -88,26 +88,5 @@ namespace MapControl { return DependencyProperty.RegisterReadOnly(name, typeof(TValue), typeof(TOwner), new PropertyMetadata(defaultValue)); } - - public static DependencyProperty AddOwner( - DependencyProperty property, - TValue defaultValue = default, - Action changed = null) - where TOwner : DependencyObject - { - var metadata = new FrameworkPropertyMetadata(); - - if (!Equals(defaultValue, property.GetMetadata(typeof(TOwner)).DefaultValue)) - { - metadata.DefaultValue = defaultValue; - } - - if (changed != null) - { - metadata.PropertyChangedCallback = (o, e) => changed((TOwner)o, (TValue)e.OldValue, (TValue)e.NewValue); - } - - return property.AddOwner(typeof(TOwner), metadata); - } } } diff --git a/MapControl/WPF/MapBase.WPF.cs b/MapControl/WPF/MapBase.WPF.cs index b1a91aa2..2aef935f 100644 --- a/MapControl/WPF/MapBase.WPF.cs +++ b/MapControl/WPF/MapBase.WPF.cs @@ -8,7 +8,7 @@ namespace MapControl public partial class MapBase { public static readonly DependencyProperty ForegroundProperty = - DependencyPropertyHelper.AddOwner(TextElement.ForegroundProperty, Brushes.Black); + TextElement.ForegroundProperty.AddOwner(typeof(MapBase), new FrameworkPropertyMetadata(Brushes.Black)); public static readonly DependencyProperty AnimationEasingFunctionProperty = DependencyPropertyHelper.Register(nameof(AnimationEasingFunction), diff --git a/MapControl/WPF/MapContentControl.WPF.cs b/MapControl/WPF/MapContentControl.WPF.cs index 16a1af9b..0c1e4f24 100644 --- a/MapControl/WPF/MapContentControl.WPF.cs +++ b/MapControl/WPF/MapContentControl.WPF.cs @@ -9,10 +9,10 @@ namespace MapControl public class MapContentControl : ContentControl { public static readonly DependencyProperty AutoCollapseProperty = - DependencyPropertyHelper.AddOwner(MapPanel.AutoCollapseProperty); + MapPanel.AutoCollapseProperty.AddOwner(typeof(MapContentControl)); public static readonly DependencyProperty LocationProperty = - DependencyPropertyHelper.AddOwner(MapPanel.LocationProperty); + MapPanel.LocationProperty.AddOwner(typeof(MapContentControl)); static MapContentControl() { diff --git a/MapControl/WPF/MapGraticule.WPF.cs b/MapControl/WPF/MapGraticule.WPF.cs index 4442ee44..059e1254 100644 --- a/MapControl/WPF/MapGraticule.WPF.cs +++ b/MapControl/WPF/MapGraticule.WPF.cs @@ -2,13 +2,22 @@ using System.Globalization; using System.Linq; using System.Windows; -using System.Windows.Controls; +using System.Windows.Documents; using System.Windows.Media; namespace MapControl { - public partial class MapGraticule : Control, IMapElement + public partial class MapGraticule : FrameworkElement, IMapElement { + public static readonly DependencyProperty ForegroundProperty = + TextElement.ForegroundProperty.AddOwner(typeof(MapGraticule)); + + public static readonly DependencyProperty FontFamilyProperty = + TextElement.FontFamilyProperty.AddOwner(typeof(MapGraticule)); + + public static readonly DependencyProperty FontSizeProperty = + TextElement.FontSizeProperty.AddOwner(typeof(MapGraticule), new FrameworkPropertyMetadata(12d)); + private MapBase parentMap; /// @@ -56,7 +65,7 @@ namespace MapControl if (labels.Count > 0) { - var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch); + var typeface = new Typeface(FontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal); var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip; foreach (var label in labels) diff --git a/MapControl/WPF/MapItem.WPF.cs b/MapControl/WPF/MapItem.WPF.cs index f74531a3..baffc5c7 100644 --- a/MapControl/WPF/MapItem.WPF.cs +++ b/MapControl/WPF/MapItem.WPF.cs @@ -7,11 +7,11 @@ namespace MapControl public partial class MapItem { public static readonly DependencyProperty AutoCollapseProperty = - DependencyPropertyHelper.AddOwner(MapPanel.AutoCollapseProperty); + MapPanel.AutoCollapseProperty.AddOwner(typeof(MapItem)); public static readonly DependencyProperty LocationProperty = - DependencyPropertyHelper.AddOwner(MapPanel.LocationProperty, null, - (item, oldValue, newValue) => item.UpdateMapTransform()); + MapPanel.LocationProperty.AddOwner(typeof(MapItem), + new FrameworkPropertyMetadata(null, (o, e) => ((MapItem)o).UpdateMapTransform())); static MapItem() { diff --git a/MapControl/WPF/MapPath.WPF.cs b/MapControl/WPF/MapPath.WPF.cs index 4e637757..b0a6032c 100644 --- a/MapControl/WPF/MapPath.WPF.cs +++ b/MapControl/WPF/MapPath.WPF.cs @@ -6,14 +6,9 @@ namespace MapControl { public partial class MapPath : Shape { - public MapPath() - { - Stretch = Stretch.None; - } - public static readonly DependencyProperty DataProperty = - DependencyPropertyHelper.AddOwner(Path.DataProperty, null, - (path, oldValue, newValue) => path.DataPropertyChanged(oldValue, newValue)); + Path.DataProperty.AddOwner(typeof(MapPath), + new FrameworkPropertyMetadata(null, (o, e) => ((MapPath)o).DataPropertyChanged(e))); public Geometry Data { @@ -23,15 +18,17 @@ namespace MapControl protected override Geometry DefiningGeometry => Data; - private void DataPropertyChanged(Geometry oldData, Geometry newData) + private void DataPropertyChanged(DependencyPropertyChangedEventArgs e) { - // Check if data is actually a new Geometry. + // Check if Data is actually a new Geometry. // - if (newData != null && !ReferenceEquals(newData, oldData)) + if (e.NewValue != null && !ReferenceEquals(e.NewValue, e.OldValue)) { - if (newData.IsFrozen) + var data = (Geometry)e.NewValue; + + if (data.IsFrozen) { - Data = newData.Clone(); // DataPropertyChanged called again + Data = data.Clone(); // DataPropertyChanged called again } else { diff --git a/MapControl/WinUI/MapGraticule.WinUI.cs b/MapControl/WinUI/MapGraticule.WinUI.cs index 4282831d..fec42894 100644 --- a/MapControl/WinUI/MapGraticule.WinUI.cs +++ b/MapControl/WinUI/MapGraticule.WinUI.cs @@ -28,24 +28,6 @@ namespace MapControl public static readonly DependencyProperty FontSizeProperty = DependencyPropertyHelper.Register(nameof(FontSize), 12d); - public Brush Foreground - { - get => (Brush)GetValue(ForegroundProperty); - set => SetValue(ForegroundProperty, value); - } - - public FontFamily FontFamily - { - get => (FontFamily)GetValue(FontFamilyProperty); - set => SetValue(FontFamilyProperty, value); - } - - public double FontSize - { - get => (double)GetValue(FontSizeProperty); - set => SetValue(FontSizeProperty, value); - } - protected override void SetParentMap(MapBase map) { if (map != null && Foreground == null) diff --git a/MapControl/WinUI/MapPath.WinUI.cs b/MapControl/WinUI/MapPath.WinUI.cs index 7fe6284e..9ed11c2f 100644 --- a/MapControl/WinUI/MapPath.WinUI.cs +++ b/MapControl/WinUI/MapPath.WinUI.cs @@ -12,7 +12,6 @@ namespace MapControl { public MapPath() { - Stretch = Stretch.None; MapPanel.InitMapElement(this); }