From a3d25b50846af132e6ada87e12816a081a4c0a43 Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Mon, 2 Feb 2026 14:52:58 +0100 Subject: [PATCH] Added DependencyPropertyHelper.AddOwner --- .../DependencyPropertyHelper.Avalonia.cs | 16 ++++++ MapControl/Avalonia/MapBase.Avalonia.cs | 4 +- .../Avalonia/MapContentControl.Avalonia.cs | 36 ------------- MapControl/Avalonia/MapItem.Avalonia.cs | 10 +--- MapControl/Shared/MapContentControl.cs | 54 +++++++++++++++++++ MapControl/Shared/MapItem.cs | 11 ++++ .../WPF/DependencyPropertyHelper.WPF.cs | 17 +++++- MapControl/WPF/MapContentControl.WPF.cs | 35 +----------- MapControl/WPF/MapItem.WPF.cs | 7 --- .../WinUI/DependencyPropertyHelper.WinUI.cs | 15 ++++++ MapControl/WinUI/MapContentControl.WinUI.cs | 35 +----------- MapControl/WinUI/MapItem.WinUI.cs | 12 ----- 12 files changed, 119 insertions(+), 133 deletions(-) create mode 100644 MapControl/Shared/MapContentControl.cs diff --git a/MapControl/Avalonia/DependencyPropertyHelper.Avalonia.cs b/MapControl/Avalonia/DependencyPropertyHelper.Avalonia.cs index bbb1c621..57d2adc0 100644 --- a/MapControl/Avalonia/DependencyPropertyHelper.Avalonia.cs +++ b/MapControl/Avalonia/DependencyPropertyHelper.Avalonia.cs @@ -74,11 +74,27 @@ namespace MapControl Action changed) where TOwner : AvaloniaObject { var property = source.AddOwner(); + property.Changed.AddClassHandler((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value)); return property; } + public static StyledProperty AddOwner( + string _, // for compatibility with WinUI/UWP DependencyPropertyHelper + StyledProperty source) where TOwner : AvaloniaObject + { + return AddOwner(source); + } + + public static StyledProperty AddOwner( + string _, // for compatibility with WinUI/UWP DependencyPropertyHelper + StyledProperty source, + Action changed) where TOwner : AvaloniaObject + { + return AddOwner(source, changed); + } + 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 2e9ac38a..6a0534a2 100644 --- a/MapControl/Avalonia/MapBase.Avalonia.cs +++ b/MapControl/Avalonia/MapBase.Avalonia.cs @@ -78,8 +78,8 @@ namespace MapControl static MapBase() { - BackgroundProperty.OverrideDefaultValue(typeof(MapBase), Brushes.White); - ClipToBoundsProperty.OverrideDefaultValue(typeof(MapBase), true); + BackgroundProperty.OverrideDefaultValue(Brushes.White); + ClipToBoundsProperty.OverrideDefaultValue(true); Animation.RegisterCustomAnimator(); } diff --git a/MapControl/Avalonia/MapContentControl.Avalonia.cs b/MapControl/Avalonia/MapContentControl.Avalonia.cs index 9350bdf8..f8499236 100644 --- a/MapControl/Avalonia/MapContentControl.Avalonia.cs +++ b/MapControl/Avalonia/MapContentControl.Avalonia.cs @@ -3,40 +3,4 @@ using Avalonia.Controls; namespace MapControl { - /// - /// ContentControl placed on a MapPanel at a geographic location specified by the Location property. - /// - public class MapContentControl : ContentControl - { - public static readonly StyledProperty LocationProperty = - DependencyPropertyHelper.AddOwner(MapPanel.LocationProperty); - - public static readonly StyledProperty AutoCollapseProperty = - DependencyPropertyHelper.AddOwner(MapPanel.AutoCollapseProperty); - - /// - /// Gets/sets MapPanel.Location. - /// - public Location Location - { - get => GetValue(LocationProperty); - set => SetValue(LocationProperty, value); - } - - /// - /// Gets/sets MapPanel.AutoCollapse. - /// - public bool AutoCollapse - { - get => GetValue(AutoCollapseProperty); - set => SetValue(AutoCollapseProperty, value); - } - } - - /// - /// MapContentControl with a Pushpin Style. - /// - public class Pushpin : MapContentControl - { - } } diff --git a/MapControl/Avalonia/MapItem.Avalonia.cs b/MapControl/Avalonia/MapItem.Avalonia.cs index 30ba4902..f30774cc 100644 --- a/MapControl/Avalonia/MapItem.Avalonia.cs +++ b/MapControl/Avalonia/MapItem.Avalonia.cs @@ -1,18 +1,10 @@ -using Avalonia; -using Avalonia.Controls; +using Avalonia.Controls; using Avalonia.Input; namespace MapControl { public partial class MapItem { - public static readonly StyledProperty LocationProperty = - DependencyPropertyHelper.AddOwner(MapPanel.LocationProperty, - (item, oldValue, newValue) => item.UpdateMapTransform()); - - public static readonly StyledProperty AutoCollapseProperty = - DependencyPropertyHelper.AddOwner(MapPanel.AutoCollapseProperty); - protected override void OnPointerPressed(PointerPressedEventArgs e) { if (e.Pointer.Type != PointerType.Mouse && diff --git a/MapControl/Shared/MapContentControl.cs b/MapControl/Shared/MapContentControl.cs new file mode 100644 index 00000000..9be68987 --- /dev/null +++ b/MapControl/Shared/MapContentControl.cs @@ -0,0 +1,54 @@ +#if WPF +using System.Windows; +using System.Windows.Controls; +#elif UWP +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +#elif WINUI +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +#elif AVALONIA +using Avalonia.Controls; +#endif + +namespace MapControl +{ + /// + /// ContentControl placed on a MapPanel at a geographic location specified by the Location property. + /// + public partial class MapContentControl : ContentControl + { + public static readonly DependencyProperty LocationProperty = + DependencyPropertyHelper.AddOwner( + nameof(Location), MapPanel.LocationProperty); + + public static readonly DependencyProperty AutoCollapseProperty = + DependencyPropertyHelper.AddOwner( + nameof(AutoCollapse), MapPanel.AutoCollapseProperty); + + /// + /// Gets/sets MapPanel.Location. + /// + public Location Location + { + get => (Location)GetValue(LocationProperty); + set => SetValue(LocationProperty, value); + } + + /// + /// Gets/sets MapPanel.AutoCollapse. + /// + public bool AutoCollapse + { + get => (bool)GetValue(AutoCollapseProperty); + set => SetValue(AutoCollapseProperty, value); + } + } + + /// + /// MapContentControl with a Pushpin Style. + /// + public partial class Pushpin : MapContentControl + { + } +} diff --git a/MapControl/Shared/MapItem.cs b/MapControl/Shared/MapItem.cs index 6f456a28..48e17f50 100644 --- a/MapControl/Shared/MapItem.cs +++ b/MapControl/Shared/MapItem.cs @@ -1,10 +1,13 @@ #if WPF +using System.Windows; using System.Windows.Controls; using System.Windows.Media; #elif UWP +using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; #elif WINUI +using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; #elif AVALONIA @@ -19,6 +22,14 @@ namespace MapControl /// public partial class MapItem : ListBoxItem, IMapElement { + public static readonly DependencyProperty LocationProperty = + DependencyPropertyHelper.AddOwner( + nameof(Location), MapPanel.LocationProperty, (item, _, _) => item.UpdateMapTransform()); + + public static readonly DependencyProperty AutoCollapseProperty = + DependencyPropertyHelper.AddOwner( + nameof(AutoCollapse), MapPanel.AutoCollapseProperty); + /// /// Gets/sets MapPanel.Location. /// diff --git a/MapControl/WPF/DependencyPropertyHelper.WPF.cs b/MapControl/WPF/DependencyPropertyHelper.WPF.cs index e3d5c0cb..12b71025 100644 --- a/MapControl/WPF/DependencyPropertyHelper.WPF.cs +++ b/MapControl/WPF/DependencyPropertyHelper.WPF.cs @@ -104,10 +104,25 @@ namespace MapControl public static DependencyProperty AddOwner( DependencyProperty source, - Action changed = null) where TOwner : DependencyObject + Action changed) where TOwner : DependencyObject { return source.AddOwner(typeof(TOwner), new FrameworkPropertyMetadata( (o, e) => changed((TOwner)o, (TValue)e.OldValue, (TValue)e.NewValue))); } + + public static DependencyProperty AddOwner( + string _, // for compatibility with WinUI/UWP DependencyPropertyHelper + DependencyProperty source) where TOwner : DependencyObject + { + return AddOwner(source); + } + + public static DependencyProperty AddOwner( + string _, // for compatibility with WinUI/UWP DependencyPropertyHelper + DependencyProperty source, + Action changed) where TOwner : DependencyObject + { + return AddOwner(source, changed); + } } } diff --git a/MapControl/WPF/MapContentControl.WPF.cs b/MapControl/WPF/MapContentControl.WPF.cs index 4bc62e85..be911d1a 100644 --- a/MapControl/WPF/MapContentControl.WPF.cs +++ b/MapControl/WPF/MapContentControl.WPF.cs @@ -1,47 +1,16 @@ using System.Windows; -using System.Windows.Controls; namespace MapControl { - /// - /// ContentControl placed on a MapPanel at a geographic location specified by the Location property. - /// - public class MapContentControl : ContentControl + public partial class MapContentControl { - public static readonly DependencyProperty LocationProperty = - DependencyPropertyHelper.AddOwner(MapPanel.LocationProperty); - - public static readonly DependencyProperty AutoCollapseProperty = - DependencyPropertyHelper.AddOwner(MapPanel.AutoCollapseProperty); - - /// - /// Gets/sets MapPanel.Location. - /// - public Location Location - { - get => (Location)GetValue(LocationProperty); - set => SetValue(LocationProperty, value); - } - - /// - /// Gets/sets MapPanel.AutoCollapse. - /// - public bool AutoCollapse - { - get => (bool)GetValue(AutoCollapseProperty); - set => SetValue(AutoCollapseProperty, value); - } - static MapContentControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MapContentControl), new FrameworkPropertyMetadata(typeof(MapContentControl))); } } - /// - /// MapContentControl with a Pushpin Style. - /// - public class Pushpin : MapContentControl + public partial class Pushpin { static Pushpin() { diff --git a/MapControl/WPF/MapItem.WPF.cs b/MapControl/WPF/MapItem.WPF.cs index eda7b2e1..75444644 100644 --- a/MapControl/WPF/MapItem.WPF.cs +++ b/MapControl/WPF/MapItem.WPF.cs @@ -6,13 +6,6 @@ namespace MapControl { public partial class MapItem { - public static readonly DependencyProperty LocationProperty = - DependencyPropertyHelper.AddOwner(MapPanel.LocationProperty, - (item, oldValue, newValue) => item.UpdateMapTransform()); - - public static readonly DependencyProperty AutoCollapseProperty = - DependencyPropertyHelper.AddOwner(MapPanel.AutoCollapseProperty); - static MapItem() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MapItem), new FrameworkPropertyMetadata(typeof(MapItem))); diff --git a/MapControl/WinUI/DependencyPropertyHelper.WinUI.cs b/MapControl/WinUI/DependencyPropertyHelper.WinUI.cs index 0f2dfa75..7a178c4c 100644 --- a/MapControl/WinUI/DependencyPropertyHelper.WinUI.cs +++ b/MapControl/WinUI/DependencyPropertyHelper.WinUI.cs @@ -40,5 +40,20 @@ namespace MapControl return DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), metadata); } + + public static DependencyProperty AddOwner( + string name, + DependencyProperty source, + Action changed = null) + where TOwner : DependencyObject + { + var metadata = new PropertyMetadata(default, (o, e) => + { + o.SetValue(source, e.NewValue); + changed?.Invoke((TOwner)o, (TValue)e.OldValue, (TValue)e.NewValue); + }); + + return DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), metadata); + } } } diff --git a/MapControl/WinUI/MapContentControl.WinUI.cs b/MapControl/WinUI/MapContentControl.WinUI.cs index 19af4a68..0ae4dc37 100644 --- a/MapControl/WinUI/MapContentControl.WinUI.cs +++ b/MapControl/WinUI/MapContentControl.WinUI.cs @@ -1,43 +1,15 @@ #if UWP using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Data; #else using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Data; #endif namespace MapControl { - public partial class MapContentControl : ContentControl + public partial class MapContentControl { - public static readonly DependencyProperty LocationProperty = - DependencyPropertyHelper.Register(nameof(Location), null, - (control, oldValue, newValue) => MapPanel.SetLocation(control, newValue)); - - public static readonly DependencyProperty AutoCollapseProperty = - DependencyPropertyHelper.Register(nameof(AutoCollapse), false, - (control, oldValue, newValue) => MapPanel.SetAutoCollapse(control, newValue)); - - /// - /// Gets/sets MapPanel.Location. - /// - public Location Location - { - get => (Location)GetValue(LocationProperty); - set => SetValue(LocationProperty, value); - } - - /// - /// Gets/sets MapPanel.AutoCollapse. - /// - public bool AutoCollapse - { - get => (bool)GetValue(AutoCollapseProperty); - set => SetValue(AutoCollapseProperty, value); - } - public MapContentControl() { DefaultStyleKey = typeof(MapContentControl); @@ -73,10 +45,7 @@ namespace MapControl } } - /// - /// MapContentControl with a Pushpin Style. - /// - public partial class Pushpin : MapContentControl + public partial class Pushpin { public Pushpin() { diff --git a/MapControl/WinUI/MapItem.WinUI.cs b/MapControl/WinUI/MapItem.WinUI.cs index f027598f..d99928dc 100644 --- a/MapControl/WinUI/MapItem.WinUI.cs +++ b/MapControl/WinUI/MapItem.WinUI.cs @@ -16,18 +16,6 @@ namespace MapControl { public partial class MapItem { - public static readonly DependencyProperty LocationProperty = - DependencyPropertyHelper.Register(nameof(Location), null, - (item, oldValue, newValue) => - { - MapPanel.SetLocation(item, newValue); - item.UpdateMapTransform(); - }); - - public static readonly DependencyProperty AutoCollapseProperty = - DependencyPropertyHelper.Register(nameof(AutoCollapse), false, - (item, oldValue, newValue) => MapPanel.SetAutoCollapse(item, newValue)); - private Windows.Foundation.Point? pointerPressedPosition; public MapItem()