diff --git a/MBTiles/Shared/MBTileLayer.cs b/MBTiles/Shared/MBTileLayer.cs index 233c406e..f6060a42 100644 --- a/MBTiles/Shared/MBTileLayer.cs +++ b/MBTiles/Shared/MBTileLayer.cs @@ -18,9 +18,9 @@ namespace MapControl.MBTiles /// public class MBTileLayer : MapTileLayer { - public static readonly DependencyProperty FileProperty = DependencyProperty.Register( - nameof(File), typeof(string), typeof(MBTileLayer), - new PropertyMetadata(null, async (o, e) => await ((MBTileLayer)o).FilePropertyChanged((string)e.NewValue))); + public static readonly DependencyProperty FileProperty = + DependencyPropertyHelper.Register(nameof(File), null, false, + async (layer, oldValue, newValue) => await layer.FilePropertyChanged(newValue)); public string File { diff --git a/MapControl/Shared/GeoImage.cs b/MapControl/Shared/GeoImage.cs index 07857366..e7cfb2b7 100644 --- a/MapControl/Shared/GeoImage.cs +++ b/MapControl/Shared/GeoImage.cs @@ -51,9 +51,9 @@ namespace MapControl private static string QueryString(ushort tag) => $"/ifd/{{ushort={tag}}}"; - public static readonly DependencyProperty SourcePathProperty = DependencyProperty.Register( - nameof(SourcePath), typeof(string), typeof(GeoImage), - new PropertyMetadata(null, async (o, e) => await ((GeoImage)o).SourcePathPropertyChanged((string)e.NewValue))); + public static readonly DependencyProperty SourcePathProperty = + DependencyPropertyHelper.Register(nameof(SourcePath), null, false, + async (image, oldValue, newValue) => await image.SourcePathPropertyChanged(newValue)); public GeoImage() { diff --git a/MapControl/Shared/MapGraticule.cs b/MapControl/Shared/MapGraticule.cs index 7c68b81f..1a198365 100644 --- a/MapControl/Shared/MapGraticule.cs +++ b/MapControl/Shared/MapGraticule.cs @@ -44,8 +44,8 @@ namespace MapControl private const double LineInterpolationResolution = 2d; - public static readonly DependencyProperty MinLineDistanceProperty = DependencyProperty.Register( - nameof(MinLineDistance), typeof(double), typeof(MapGraticule), new PropertyMetadata(150d)); + public static readonly DependencyProperty MinLineDistanceProperty = + DependencyPropertyHelper.Register(nameof(MinLineDistance), 150d); private double lineDistance; private string labelFormat; diff --git a/MapControl/Shared/MapItemsControl.cs b/MapControl/Shared/MapItemsControl.cs index 2d84370d..2232a05b 100644 --- a/MapControl/Shared/MapItemsControl.cs +++ b/MapControl/Shared/MapItemsControl.cs @@ -24,8 +24,8 @@ namespace MapControl /// public partial class MapItemsControl : ListBox { - public static readonly DependencyProperty LocationMemberPathProperty = DependencyProperty.Register( - nameof(LocationMemberPath), typeof(string), typeof(MapItemsControl), new PropertyMetadata(null)); + public static readonly DependencyProperty LocationMemberPathProperty = + DependencyPropertyHelper.Register(nameof(LocationMemberPath)); /// /// Path to a source property for binding the Location property of MapItem containers. diff --git a/MapControl/Shared/MapPath.cs b/MapControl/Shared/MapPath.cs index 42aa5a8f..684b7800 100644 --- a/MapControl/Shared/MapPath.cs +++ b/MapControl/Shared/MapPath.cs @@ -23,9 +23,9 @@ namespace MapControl /// public partial class MapPath : IMapElement { - public static readonly DependencyProperty LocationProperty = DependencyProperty.Register( - nameof(Location), typeof(Location), typeof(MapPath), - new PropertyMetadata(null, (o, e) => ((MapPath)o).UpdateData())); + public static readonly DependencyProperty LocationProperty = + DependencyPropertyHelper.Register(nameof(Location), null, false, + (path, oldValue, newValue) => path.UpdateData()); private MapBase parentMap; diff --git a/MapControl/Shared/MapPolygon.cs b/MapControl/Shared/MapPolygon.cs index 86931920..3737a2e6 100644 --- a/MapControl/Shared/MapPolygon.cs +++ b/MapControl/Shared/MapPolygon.cs @@ -21,13 +21,13 @@ namespace MapControl /// public class MapPolygon : MapPath { - public static readonly DependencyProperty LocationsProperty = DependencyProperty.Register( - nameof(Locations), typeof(IEnumerable), typeof(MapPolygon), - new PropertyMetadata(null, (o, e) => ((MapPolygon)o).DataCollectionPropertyChanged(e))); + public static readonly DependencyProperty LocationsProperty = + DependencyPropertyHelper.Register>(nameof(Locations), null, false, + (polygon, oldValue, newValue) => polygon.DataCollectionPropertyChanged(oldValue, newValue)); - public static readonly DependencyProperty FillRuleProperty = DependencyProperty.Register( - nameof(FillRule), typeof(FillRule), typeof(MapPolygon), - new PropertyMetadata(FillRule.EvenOdd, (o, e) => ((PathGeometry)((MapPolygon)o).Data).FillRule = (FillRule)e.NewValue)); + public static readonly DependencyProperty FillRuleProperty = + DependencyPropertyHelper.Register(nameof(FillRule), FillRule.EvenOdd, false, + (polygon, oldValue, newValue) => ((PathGeometry)polygon.Data).FillRule = newValue); /// /// Gets or sets the Locations that define the polygon points. diff --git a/MapControl/Shared/MapPolyline.cs b/MapControl/Shared/MapPolyline.cs index a8705d62..f653e9c6 100644 --- a/MapControl/Shared/MapPolyline.cs +++ b/MapControl/Shared/MapPolyline.cs @@ -21,13 +21,13 @@ namespace MapControl /// public class MapPolyline : MapPath { - public static readonly DependencyProperty LocationsProperty = DependencyProperty.Register( - nameof(Locations), typeof(IEnumerable), typeof(MapPolyline), - new PropertyMetadata(null, (o, e) => ((MapPolyline)o).DataCollectionPropertyChanged(e))); + public static readonly DependencyProperty LocationsProperty = + DependencyPropertyHelper.Register>(nameof(Locations), null, false, + (polyline, oldValue, newValue) => polyline.DataCollectionPropertyChanged(oldValue, newValue)); - public static readonly DependencyProperty FillRuleProperty = DependencyProperty.Register( - nameof(FillRule), typeof(FillRule), typeof(MapPolyline), - new PropertyMetadata(FillRule.EvenOdd, (o, e) => ((PathGeometry)((MapPolyline)o).Data).FillRule = (FillRule)e.NewValue)); + public static readonly DependencyProperty FillRuleProperty = + DependencyPropertyHelper.Register(nameof(FillRule), FillRule.EvenOdd, false, + (polyline, oldValue, newValue) => ((PathGeometry)polyline.Data).FillRule = newValue); /// /// Gets or sets the Locations that define the polyline points. diff --git a/MapControl/Shared/MapScale.cs b/MapControl/Shared/MapScale.cs index ae16dd84..a04e229b 100644 --- a/MapControl/Shared/MapScale.cs +++ b/MapControl/Shared/MapScale.cs @@ -30,8 +30,8 @@ namespace MapControl /// public class MapScale : MapOverlay { - public static readonly DependencyProperty PaddingProperty = DependencyProperty.Register( - nameof(Padding), typeof(Thickness), typeof(MapScale), new PropertyMetadata(new Thickness(4))); + public static readonly DependencyProperty PaddingProperty = + DependencyPropertyHelper.Register(nameof(Padding), new Thickness(4)); private readonly Polyline line = new Polyline(); diff --git a/MapControl/WPF/DependencyPropertyHelper.WPF.cs b/MapControl/WPF/DependencyPropertyHelper.WPF.cs index 3aeed1a2..6cf024ff 100644 --- a/MapControl/WPF/DependencyPropertyHelper.WPF.cs +++ b/MapControl/WPF/DependencyPropertyHelper.WPF.cs @@ -9,6 +9,15 @@ namespace MapControl { public static class DependencyPropertyHelper { + public static DependencyProperty Register( + string name, + TValue defaultValue, + FrameworkPropertyMetadataOptions options) + where TOwner : DependencyObject + { + return DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), new FrameworkPropertyMetadata(defaultValue, options)); + } + public static DependencyProperty Register( string name, TValue defaultValue = default, @@ -64,5 +73,35 @@ namespace MapControl { return DependencyProperty.RegisterReadOnly(name, typeof(TValue), typeof(TOwner), new PropertyMetadata(defaultValue)); } + + public static DependencyProperty AddOwner( + DependencyProperty property, + FrameworkPropertyMetadataOptions options = FrameworkPropertyMetadataOptions.None) + where TOwner : DependencyObject + { + FrameworkPropertyMetadata metadata = null; + + if (options != FrameworkPropertyMetadataOptions.None) + { + metadata = new FrameworkPropertyMetadata(property.DefaultMetadata.DefaultValue, options); + } + + return property.AddOwner(typeof(TOwner), metadata); + } + + public static DependencyProperty AddOwner( + DependencyProperty property, + Action changed) + where TOwner : DependencyObject + { + FrameworkPropertyMetadata metadata = null; + + if (changed != null) + { + metadata = new FrameworkPropertyMetadata((o, e) => changed((TOwner)o, (TValue)e.OldValue, (TValue)e.NewValue)); + } + + return property.AddOwner(typeof(TOwner), metadata); + } } } diff --git a/MapControl/WPF/Map.WPF.cs b/MapControl/WPF/Map.WPF.cs index 44a11a36..8bae6fb7 100644 --- a/MapControl/WPF/Map.WPF.cs +++ b/MapControl/WPF/Map.WPF.cs @@ -13,12 +13,11 @@ namespace MapControl /// public class Map : MapBase { - public static readonly DependencyProperty MouseWheelZoomDeltaProperty = DependencyProperty.Register( - nameof(MouseWheelZoomDelta), typeof(double), typeof(Map), new PropertyMetadata(0.25)); + public static readonly DependencyProperty MouseWheelZoomDeltaProperty = + DependencyPropertyHelper.Register(nameof(MouseWheelZoomDelta), 0.25); - public static readonly DependencyProperty ManipulationModeProperty = DependencyProperty.Register( - nameof(ManipulationMode), typeof(ManipulationModes), typeof(Map), - new PropertyMetadata(ManipulationModes.Scale | ManipulationModes.Translate)); + public static readonly DependencyProperty ManipulationModeProperty = + DependencyPropertyHelper.Register(nameof(ManipulationMode), ManipulationModes.Scale | ManipulationModes.Translate); private Point? mousePosition; private double mouseWheelDelta; diff --git a/MapControl/WPF/MapContentControl.WPF.cs b/MapControl/WPF/MapContentControl.WPF.cs index 53c7bc25..055e93c5 100644 --- a/MapControl/WPF/MapContentControl.WPF.cs +++ b/MapControl/WPF/MapContentControl.WPF.cs @@ -12,9 +12,11 @@ namespace MapControl /// public class MapContentControl : ContentControl { - public static readonly DependencyProperty AutoCollapseProperty = MapPanel.AutoCollapseProperty.AddOwner(typeof(MapContentControl)); + public static readonly DependencyProperty AutoCollapseProperty = + DependencyPropertyHelper.AddOwner(MapPanel.AutoCollapseProperty); - public static readonly DependencyProperty LocationProperty = MapPanel.LocationProperty.AddOwner(typeof(MapContentControl)); + public static readonly DependencyProperty LocationProperty = + DependencyPropertyHelper.AddOwner(MapPanel.LocationProperty); static MapContentControl() { @@ -50,8 +52,8 @@ namespace MapControl DefaultStyleKeyProperty.OverrideMetadata(typeof(Pushpin), new FrameworkPropertyMetadata(typeof(Pushpin))); } - public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register( - nameof(CornerRadius), typeof(CornerRadius), typeof(Pushpin)); + public static readonly DependencyProperty CornerRadiusProperty = + DependencyPropertyHelper.Register(nameof(CornerRadius)); public CornerRadius CornerRadius { diff --git a/MapControl/WPF/MapItem.WPF.cs b/MapControl/WPF/MapItem.WPF.cs index 47f76800..14e20460 100644 --- a/MapControl/WPF/MapItem.WPF.cs +++ b/MapControl/WPF/MapItem.WPF.cs @@ -10,12 +10,12 @@ namespace MapControl { public partial class MapItem { - public static readonly DependencyProperty AutoCollapseProperty = MapPanel.AutoCollapseProperty.AddOwner( - typeof(MapItem)); + public static readonly DependencyProperty AutoCollapseProperty = + DependencyPropertyHelper.AddOwner(MapPanel.AutoCollapseProperty); - public static readonly DependencyProperty LocationProperty = MapPanel.LocationProperty.AddOwner( - typeof(MapItem), new FrameworkPropertyMetadata(null, - (o, e) => ((MapItem)o).UpdateMapTransform((Location)e.NewValue))); + public static readonly DependencyProperty LocationProperty = + DependencyPropertyHelper.AddOwner(MapPanel.LocationProperty, + (item, oldValue, newValue) => item.UpdateMapTransform(newValue)); static MapItem() { diff --git a/MapControl/WPF/MapItemsImageLayer.WPF.cs b/MapControl/WPF/MapItemsImageLayer.WPF.cs index 1bb76ba1..00733dd4 100644 --- a/MapControl/WPF/MapItemsImageLayer.WPF.cs +++ b/MapControl/WPF/MapItemsImageLayer.WPF.cs @@ -20,8 +20,8 @@ namespace MapControl public class MapItemsImageLayer : MapImageLayer { - public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register( - nameof(ItemsSource), typeof(IEnumerable), typeof(MapItemsImageLayer)); + public static readonly DependencyProperty ItemsSourceProperty = + DependencyPropertyHelper.Register>(nameof(ItemsSource)); public IEnumerable ItemsSource { diff --git a/MapControl/WPF/MapMultiPolygon.WPF.cs b/MapControl/WPF/MapMultiPolygon.WPF.cs index eb79dc44..c8ff4f03 100644 --- a/MapControl/WPF/MapMultiPolygon.WPF.cs +++ b/MapControl/WPF/MapMultiPolygon.WPF.cs @@ -18,13 +18,13 @@ namespace MapControl /// public class MapMultiPolygon : MapPath { - public static readonly DependencyProperty PolygonsProperty = DependencyProperty.Register( - nameof(Polygons), typeof(IEnumerable>), typeof(MapMultiPolygon), - new PropertyMetadata(null, (o, e) => ((MapMultiPolygon)o).DataCollectionPropertyChanged(e))); + public static readonly DependencyProperty PolygonsProperty = + DependencyPropertyHelper.Register>>(nameof(Polygons), null, false, + (polygon, oldValue, newValue) => polygon.DataCollectionPropertyChanged(oldValue, newValue)); - public static readonly DependencyProperty FillRuleProperty = DependencyProperty.Register( - nameof(FillRule), typeof(FillRule), typeof(MapMultiPolygon), - new PropertyMetadata(FillRule.EvenOdd, (o, e) => ((PathGeometry)((MapMultiPolygon)o).Data).FillRule = (FillRule)e.NewValue)); + public static readonly DependencyProperty FillRuleProperty = + DependencyPropertyHelper.Register(nameof(FillRule), FillRule.EvenOdd, false, + (polygon, oldValue, newValue) => ((PathGeometry)polygon.Data).FillRule = newValue); /// /// Gets or sets the Locations that define the multi-polygon points. diff --git a/MapControl/WPF/MapOverlay.WPF.cs b/MapControl/WPF/MapOverlay.WPF.cs index a28bf4cf..f82a219a 100644 --- a/MapControl/WPF/MapOverlay.WPF.cs +++ b/MapControl/WPF/MapOverlay.WPF.cs @@ -12,50 +12,65 @@ namespace MapControl { public partial class MapOverlay { - public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true, Inherits = true }); + public static readonly DependencyProperty FontFamilyProperty = + DependencyPropertyHelper.AddOwner(TextElement.FontFamilyProperty, + FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits); - public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true, Inherits = true }); + public static readonly DependencyProperty FontSizeProperty = + DependencyPropertyHelper.AddOwner(TextElement.FontSizeProperty, + FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits); - public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true, Inherits = true }); + public static readonly DependencyProperty FontStyleProperty = + DependencyPropertyHelper.AddOwner(TextElement.FontStyleProperty, + FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits); - public static readonly DependencyProperty FontStretchProperty = TextElement.FontStretchProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true, Inherits = true }); + public static readonly DependencyProperty FontStretchProperty = + DependencyPropertyHelper.AddOwner(TextElement.FontStretchProperty, + FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits); - public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true, Inherits = true }); + public static readonly DependencyProperty FontWeightProperty = + DependencyPropertyHelper.AddOwner(TextElement.FontWeightProperty, + FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits); - public static readonly DependencyProperty ForegroundProperty = TextElement.ForegroundProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true, Inherits = true }); + public static readonly DependencyProperty ForegroundProperty = + DependencyPropertyHelper.AddOwner(TextElement.ForegroundProperty, + FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits); - public static readonly DependencyProperty StrokeProperty = Shape.StrokeProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); + public static readonly DependencyProperty StrokeProperty = + DependencyPropertyHelper.AddOwner(Shape.StrokeProperty, + FrameworkPropertyMetadataOptions.AffectsRender); - public static readonly DependencyProperty StrokeThicknessProperty = Shape.StrokeThicknessProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); + public static readonly DependencyProperty StrokeThicknessProperty = + DependencyPropertyHelper.AddOwner(Shape.StrokeThicknessProperty, + FrameworkPropertyMetadataOptions.AffectsRender); - public static readonly DependencyProperty StrokeDashArrayProperty = Shape.StrokeDashArrayProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); + public static readonly DependencyProperty StrokeDashArrayProperty = + DependencyPropertyHelper.AddOwner(Shape.StrokeDashArrayProperty, + FrameworkPropertyMetadataOptions.AffectsRender); - public static readonly DependencyProperty StrokeDashOffsetProperty = Shape.StrokeDashOffsetProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); + public static readonly DependencyProperty StrokeDashOffsetProperty = + DependencyPropertyHelper.AddOwner(Shape.StrokeDashOffsetProperty, + FrameworkPropertyMetadataOptions.AffectsRender); - public static readonly DependencyProperty StrokeDashCapProperty = Shape.StrokeDashCapProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); + public static readonly DependencyProperty StrokeDashCapProperty = + DependencyPropertyHelper.AddOwner(Shape.StrokeDashCapProperty, + FrameworkPropertyMetadataOptions.AffectsRender); - public static readonly DependencyProperty StrokeStartLineCapProperty = Shape.StrokeStartLineCapProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); + public static readonly DependencyProperty StrokeStartLineCapProperty = + DependencyPropertyHelper.AddOwner(Shape.StrokeStartLineCapProperty, + FrameworkPropertyMetadataOptions.AffectsRender); - public static readonly DependencyProperty StrokeEndLineCapProperty = Shape.StrokeEndLineCapProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); + public static readonly DependencyProperty StrokeEndLineCapProperty = + DependencyPropertyHelper.AddOwner(Shape.StrokeEndLineCapProperty, + FrameworkPropertyMetadataOptions.AffectsRender); - public static readonly DependencyProperty StrokeLineJoinProperty = Shape.StrokeLineJoinProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); + public static readonly DependencyProperty StrokeLineJoinProperty = + DependencyPropertyHelper.AddOwner(Shape.StrokeLineJoinProperty, + FrameworkPropertyMetadataOptions.AffectsRender); - public static readonly DependencyProperty StrokeMiterLimitProperty = Shape.StrokeMiterLimitProperty.AddOwner( - typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); + public static readonly DependencyProperty StrokeMiterLimitProperty = + DependencyPropertyHelper.AddOwner(Shape.StrokeMiterLimitProperty, + FrameworkPropertyMetadataOptions.AffectsRender); protected override void OnInitialized(EventArgs e) { diff --git a/MapControl/WPF/MapPath.WPF.cs b/MapControl/WPF/MapPath.WPF.cs index 9384b719..794037ca 100644 --- a/MapControl/WPF/MapPath.WPF.cs +++ b/MapControl/WPF/MapPath.WPF.cs @@ -3,6 +3,7 @@ // Licensed under the Microsoft Public License (Ms-PL) using System; +using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; @@ -19,8 +20,9 @@ namespace MapControl Stretch = Stretch.None; } - public static readonly DependencyProperty DataProperty = Path.DataProperty.AddOwner( - typeof(MapPath), new PropertyMetadata(null, DataPropertyChanged)); + public static readonly DependencyProperty DataProperty = + DependencyPropertyHelper.AddOwner(Path.DataProperty, + (path, oldValue, newValue) => path.DataPropertyChanged(oldValue, newValue)); public Geometry Data { @@ -30,23 +32,19 @@ namespace MapControl protected override Geometry DefiningGeometry => Data; - private static void DataPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) + private void DataPropertyChanged(Geometry oldData, Geometry newData) { - var data = (Geometry)e.NewValue; - // Check if data is actually a new Geometry. // - if (data != null && !ReferenceEquals(data, e.OldValue)) + if (newData != null && !ReferenceEquals(newData, oldData)) { - var path = (MapPath)obj; - - if (data.IsFrozen) + if (newData.IsFrozen) { - path.Data = data.Clone(); // DataPropertyChanged called again + Data = newData.Clone(); // DataPropertyChanged called again } else { - path.UpdateData(); + UpdateData(); } } } @@ -65,14 +63,14 @@ namespace MapControl #region Methods used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon - protected void DataCollectionPropertyChanged(DependencyPropertyChangedEventArgs e) + protected void DataCollectionPropertyChanged(IEnumerable oldValue, IEnumerable newValue) { - if (e.OldValue is INotifyCollectionChanged oldCollection) + if (oldValue is INotifyCollectionChanged oldCollection) { CollectionChangedEventManager.RemoveListener(oldCollection, this); } - if (e.NewValue is INotifyCollectionChanged newCollection) + if (newValue is INotifyCollectionChanged newCollection) { CollectionChangedEventManager.AddListener(newCollection, this); } diff --git a/MapControl/WPF/PushpinBorder.WPF.cs b/MapControl/WPF/PushpinBorder.WPF.cs index 18f58586..031bd500 100644 --- a/MapControl/WPF/PushpinBorder.WPF.cs +++ b/MapControl/WPF/PushpinBorder.WPF.cs @@ -11,35 +11,29 @@ namespace MapControl { public partial class PushpinBorder : Decorator { - public static readonly DependencyProperty ArrowSizeProperty = DependencyProperty.Register( - nameof(ArrowSize), typeof(Size), typeof(PushpinBorder), - new FrameworkPropertyMetadata(new Size(10d, 20d), - FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender)); + public static readonly DependencyProperty ArrowSizeProperty = + DependencyPropertyHelper.Register(nameof(ArrowSize), new Size(10d, 20d), + FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender); - public static readonly DependencyProperty BorderWidthProperty = DependencyProperty.Register( - nameof(BorderWidth), typeof(double), typeof(PushpinBorder), - new FrameworkPropertyMetadata(0d, - FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender)); + public static readonly DependencyProperty BorderWidthProperty = + DependencyPropertyHelper.Register(nameof(BorderWidth), 0d, + FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender); - public static readonly DependencyProperty BackgroundProperty = DependencyProperty.Register( - nameof(Background), typeof(Brush), typeof(PushpinBorder), - new FrameworkPropertyMetadata(null, - FrameworkPropertyMetadataOptions.AffectsRender)); + public static readonly DependencyProperty BackgroundProperty = + DependencyPropertyHelper.Register(nameof(Background), null, + FrameworkPropertyMetadataOptions.AffectsRender); - public static readonly DependencyProperty BorderBrushProperty = DependencyProperty.Register( - nameof(BorderBrush), typeof(Brush), typeof(PushpinBorder), - new FrameworkPropertyMetadata(null, - FrameworkPropertyMetadataOptions.AffectsRender)); + public static readonly DependencyProperty BorderBrushProperty = + DependencyPropertyHelper.Register(nameof(BorderBrush), null, + FrameworkPropertyMetadataOptions.AffectsRender); - public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register( - nameof(CornerRadius), typeof(CornerRadius), typeof(PushpinBorder), - new FrameworkPropertyMetadata(new CornerRadius(), - FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender)); + public static readonly DependencyProperty CornerRadiusProperty = + DependencyPropertyHelper.Register(nameof(CornerRadius), new CornerRadius(), + FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender); - public static readonly DependencyProperty PaddingProperty = DependencyProperty.Register( - nameof(Padding), typeof(Thickness), typeof(PushpinBorder), - new FrameworkPropertyMetadata(new Thickness(2), - FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender)); + public static readonly DependencyProperty PaddingProperty = + DependencyPropertyHelper.Register(nameof(Padding), new Thickness(2), + FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender); public Brush Background { diff --git a/MapControl/WinUI/Map.WinUI.cs b/MapControl/WinUI/Map.WinUI.cs index a264362f..b0ffe1df 100644 --- a/MapControl/WinUI/Map.WinUI.cs +++ b/MapControl/WinUI/Map.WinUI.cs @@ -21,8 +21,8 @@ namespace MapControl /// public class Map : MapBase { - public static readonly DependencyProperty MouseWheelZoomDeltaProperty = DependencyProperty.Register( - nameof(MouseWheelZoomDelta), typeof(double), typeof(Map), new PropertyMetadata(0.25)); + public static readonly DependencyProperty MouseWheelZoomDeltaProperty = + DependencyPropertyHelper.Register(nameof(MouseWheelZoomDelta), 0.25); private bool manipulationEnabled; private double mouseWheelDelta; diff --git a/MapControl/WinUI/MapContentControl.WinUI.cs b/MapControl/WinUI/MapContentControl.WinUI.cs index 4e545b63..f491d325 100644 --- a/MapControl/WinUI/MapContentControl.WinUI.cs +++ b/MapControl/WinUI/MapContentControl.WinUI.cs @@ -17,13 +17,13 @@ namespace MapControl /// public class MapContentControl : ContentControl { - public static readonly DependencyProperty AutoCollapseProperty = DependencyProperty.Register( - nameof(AutoCollapse), typeof(bool), typeof(MapContentControl), - new PropertyMetadata(false, (o, e) => MapPanel.SetAutoCollapse((MapContentControl)o, (bool)e.NewValue))); + public static readonly DependencyProperty AutoCollapseProperty = + DependencyPropertyHelper.Register(nameof(AutoCollapse), false, false, + (control, oldValue, newValue) => MapPanel.SetAutoCollapse(control, newValue)); - public static readonly DependencyProperty LocationProperty = DependencyProperty.Register( - nameof(Location), typeof(Location), typeof(MapContentControl), - new PropertyMetadata(null, (o, e) => MapPanel.SetLocation((MapContentControl)o, (Location)e.NewValue))); + public static readonly DependencyProperty LocationProperty = + DependencyPropertyHelper.Register(nameof(Location), null, false, + (control, oldValue, newValue) => MapPanel.SetLocation(control, newValue)); public MapContentControl() { diff --git a/MapControl/WinUI/MapItem.WinUI.cs b/MapControl/WinUI/MapItem.WinUI.cs index 1b90b0d0..b0304953 100644 --- a/MapControl/WinUI/MapItem.WinUI.cs +++ b/MapControl/WinUI/MapItem.WinUI.cs @@ -17,13 +17,13 @@ namespace MapControl { public partial class MapItem { - public static readonly DependencyProperty AutoCollapseProperty = DependencyProperty.Register( - nameof(AutoCollapse), typeof(bool), typeof(MapItem), new PropertyMetadata(false, - (o, e) => MapPanel.SetAutoCollapse((MapItem)o, (bool)e.NewValue))); + public static readonly DependencyProperty AutoCollapseProperty = + DependencyPropertyHelper.Register(nameof(AutoCollapse), false, false, + (item, oldValue, newValue) => MapPanel.SetAutoCollapse(item, newValue)); - public static readonly DependencyProperty LocationProperty = DependencyProperty.Register( - nameof(Location), typeof(Location), typeof(MapItem), new PropertyMetadata(null, - (o, e) => ((MapItem)o).LocationPropertyChanged((Location)e.NewValue))); + public static readonly DependencyProperty LocationProperty = + DependencyPropertyHelper.Register(nameof(Location), null, false, + (item, oldValue, newValue) => item.LocationPropertyChanged(newValue)); private void LocationPropertyChanged(Location location) { diff --git a/MapControl/WinUI/MapOverlay.WinUI.cs b/MapControl/WinUI/MapOverlay.WinUI.cs index e0349f0f..c46e8d4a 100644 --- a/MapControl/WinUI/MapOverlay.WinUI.cs +++ b/MapControl/WinUI/MapOverlay.WinUI.cs @@ -16,50 +16,50 @@ namespace MapControl { public partial class MapOverlay { - public static readonly DependencyProperty FontFamilyProperty = DependencyProperty.Register( - nameof(FontFamily), typeof(FontFamily), typeof(MapOverlay), new PropertyMetadata(null)); + public static readonly DependencyProperty FontFamilyProperty = + DependencyPropertyHelper.Register(nameof(FontFamily)); - public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register( - nameof(FontSize), typeof(double), typeof(MapOverlay), new PropertyMetadata(12d)); + public static readonly DependencyProperty FontSizeProperty = + DependencyPropertyHelper.Register(nameof(FontSize), 12d); - public static readonly DependencyProperty FontStyleProperty = DependencyProperty.Register( - nameof(FontStyle), typeof(FontStyle), typeof(MapOverlay), new PropertyMetadata(FontStyle.Normal)); + public static readonly DependencyProperty FontStyleProperty = + DependencyPropertyHelper.Register(nameof(FontStyle), FontStyle.Normal); - public static readonly DependencyProperty FontStretchProperty = DependencyProperty.Register( - nameof(FontStretch), typeof(FontStretch), typeof(MapOverlay), new PropertyMetadata(FontStretch.Normal)); + public static readonly DependencyProperty FontStretchProperty = + DependencyPropertyHelper.Register(nameof(FontStretch), FontStretch.Normal); - public static readonly DependencyProperty FontWeightProperty = DependencyProperty.Register( - nameof(FontWeight), typeof(FontWeight), typeof(MapOverlay), new PropertyMetadata(FontWeights.Normal)); + public static readonly DependencyProperty FontWeightProperty = + DependencyPropertyHelper.Register(nameof(FontWeight), FontWeights.Normal); - public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register( - nameof(Foreground), typeof(Brush), typeof(MapOverlay), new PropertyMetadata(null)); + public static readonly DependencyProperty ForegroundProperty = + DependencyPropertyHelper.Register(nameof(Foreground)); - public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register( - nameof(Stroke), typeof(Brush), typeof(MapOverlay), new PropertyMetadata(null)); + public static readonly DependencyProperty StrokeProperty = + DependencyPropertyHelper.Register(nameof(Stroke)); - public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register( - nameof(StrokeThickness), typeof(double), typeof(MapOverlay), new PropertyMetadata(1d)); + public static readonly DependencyProperty StrokeThicknessProperty = + DependencyPropertyHelper.Register(nameof(StrokeThickness), 1d); - public static readonly DependencyProperty StrokeDashArrayProperty = DependencyProperty.Register( - nameof(StrokeDashArray), typeof(DoubleCollection), typeof(MapOverlay), new PropertyMetadata(null)); + public static readonly DependencyProperty StrokeDashArrayProperty = + DependencyPropertyHelper.Register(nameof(StrokeDashArray)); - public static readonly DependencyProperty StrokeDashOffsetProperty = DependencyProperty.Register( - nameof(StrokeDashOffset), typeof(double), typeof(MapOverlay), new PropertyMetadata(0d)); + public static readonly DependencyProperty StrokeDashOffsetProperty = + DependencyPropertyHelper.Register(nameof(StrokeDashOffset)); - public static readonly DependencyProperty StrokeDashCapProperty = DependencyProperty.Register( - nameof(StrokeDashCap), typeof(PenLineCap), typeof(MapOverlay), new PropertyMetadata(PenLineCap.Flat)); + public static readonly DependencyProperty StrokeDashCapProperty = + DependencyPropertyHelper.Register(nameof(StrokeDashCap), PenLineCap.Flat); - public static readonly DependencyProperty StrokeStartLineCapProperty = DependencyProperty.Register( - nameof(StrokeStartLineCap), typeof(PenLineCap), typeof(MapOverlay), new PropertyMetadata(PenLineCap.Flat)); + public static readonly DependencyProperty StrokeStartLineCapProperty = + DependencyPropertyHelper.Register(nameof(StrokeStartLineCap), PenLineCap.Flat); - public static readonly DependencyProperty StrokeEndLineCapProperty = DependencyProperty.Register( - nameof(StrokeEndLineCap), typeof(PenLineCap), typeof(MapOverlay), new PropertyMetadata(PenLineCap.Flat)); + public static readonly DependencyProperty StrokeEndLineCapProperty = + DependencyPropertyHelper.Register(nameof(StrokeEndLineCap), PenLineCap.Flat); - public static readonly DependencyProperty StrokeLineJoinProperty = DependencyProperty.Register( - nameof(StrokeLineJoin), typeof(PenLineJoin), typeof(MapOverlay), new PropertyMetadata(PenLineJoin.Miter)); + public static readonly DependencyProperty StrokeLineJoinProperty = + DependencyPropertyHelper.Register(nameof(StrokeLineJoin), PenLineJoin.Miter); - public static readonly DependencyProperty StrokeMiterLimitProperty = DependencyProperty.Register( - nameof(StrokeMiterLimit), typeof(double), typeof(MapOverlay), new PropertyMetadata(1d)); + public static readonly DependencyProperty StrokeMiterLimitProperty = + DependencyPropertyHelper.Register(nameof(StrokeMiterLimit), 1d); protected override void SetParentMap(MapBase map) { diff --git a/MapControl/WinUI/MapPath.WinUI.cs b/MapControl/WinUI/MapPath.WinUI.cs index e41faa51..e15a35cf 100644 --- a/MapControl/WinUI/MapPath.WinUI.cs +++ b/MapControl/WinUI/MapPath.WinUI.cs @@ -2,6 +2,7 @@ // Copyright © 2024 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) +using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; @@ -39,14 +40,14 @@ namespace MapControl #region Methods used only by derived classes MapPolyline and MapPolygon - protected void DataCollectionPropertyChanged(DependencyPropertyChangedEventArgs e) + protected void DataCollectionPropertyChanged(IEnumerable oldValue, IEnumerable newValue) { - if (e.OldValue is INotifyCollectionChanged oldCollection) + if (oldValue is INotifyCollectionChanged oldCollection) { oldCollection.CollectionChanged -= DataCollectionChanged; } - if (e.NewValue is INotifyCollectionChanged newCollection) + if (newValue is INotifyCollectionChanged newCollection) { newCollection.CollectionChanged += DataCollectionChanged; } diff --git a/MapControl/WinUI/PushpinBorder.WinUI.cs b/MapControl/WinUI/PushpinBorder.WinUI.cs index 32baf5fb..c49fed59 100644 --- a/MapControl/WinUI/PushpinBorder.WinUI.cs +++ b/MapControl/WinUI/PushpinBorder.WinUI.cs @@ -24,13 +24,13 @@ namespace MapControl [ContentProperty(Name = "Child")] public partial class PushpinBorder : UserControl { - public static readonly DependencyProperty ArrowSizeProperty = DependencyProperty.Register( - nameof(ArrowSize), typeof(Size), typeof(PushpinBorder), - new PropertyMetadata(new Size(10d, 20d), (o, e) => ((PushpinBorder)o).SetBorderMargin())); + public static readonly DependencyProperty ArrowSizeProperty = + DependencyPropertyHelper.Register(nameof(ArrowSize), new Size(10d, 20d), false, + (border, oldValue, newValue) => border.SetBorderMargin()); - public static readonly DependencyProperty BorderWidthProperty = DependencyProperty.Register( - nameof(BorderWidth), typeof(double), typeof(PushpinBorder), - new PropertyMetadata(0d, (o, e) => ((PushpinBorder)o).SetBorderMargin())); + public static readonly DependencyProperty BorderWidthProperty = + DependencyPropertyHelper.Register(nameof(BorderWidth), 0d, false, + (border, oldValue, newValue) => border.SetBorderMargin()); private readonly Border border = new Border(); diff --git a/MapUiTools/Shared/MapLayersMenuButton.cs b/MapUiTools/Shared/MapLayersMenuButton.cs index 17cecb56..68aa9f4a 100644 --- a/MapUiTools/Shared/MapLayersMenuButton.cs +++ b/MapUiTools/Shared/MapLayersMenuButton.cs @@ -49,9 +49,9 @@ namespace MapControl.UiTools ((INotifyCollectionChanged)MapOverlays).CollectionChanged += (s, e) => InitializeMenu(); } - public static readonly DependencyProperty MapProperty = DependencyProperty.Register( - nameof(Map), typeof(MapBase), typeof(MapLayersMenuButton), - new PropertyMetadata(null, (o, e) => ((MapLayersMenuButton)o).InitializeMenu())); + public static readonly DependencyProperty MapProperty = + DependencyPropertyHelper.Register(nameof(Map), null, false, + (button, oldValue, newValue) => button.InitializeMenu()); public MapBase Map { diff --git a/MapUiTools/Shared/MapProjectionsMenuButton.cs b/MapUiTools/Shared/MapProjectionsMenuButton.cs index a6486029..a5cab8d0 100644 --- a/MapUiTools/Shared/MapProjectionsMenuButton.cs +++ b/MapUiTools/Shared/MapProjectionsMenuButton.cs @@ -44,9 +44,9 @@ namespace MapControl.UiTools ((INotifyCollectionChanged)MapProjections).CollectionChanged += (s, e) => InitializeMenu(); } - public static readonly DependencyProperty MapProperty = DependencyProperty.Register( - nameof(Map), typeof(MapBase), typeof(MapProjectionsMenuButton), - new PropertyMetadata(null, (o, e) => ((MapProjectionsMenuButton)o).InitializeMenu())); + public static readonly DependencyProperty MapProperty = + DependencyPropertyHelper.Register(nameof(Map), null, false, + (button, oldValue, newValue) => button.InitializeMenu()); public MapBase Map {