Added DependencyPropertyHelper.AddOwner

This commit is contained in:
ClemensFischer 2026-02-02 14:52:58 +01:00
parent f78bcb33fa
commit a3d25b5084
12 changed files with 119 additions and 133 deletions

View file

@ -74,11 +74,27 @@ namespace MapControl
Action<TOwner, TValue, TValue> changed) where TOwner : AvaloniaObject
{
var property = source.AddOwner<TOwner>();
property.Changed.AddClassHandler<TOwner, TValue>((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value));
return property;
}
public static StyledProperty<TValue> AddOwner<TOwner, TValue>(
string _, // for compatibility with WinUI/UWP DependencyPropertyHelper
StyledProperty<TValue> source) where TOwner : AvaloniaObject
{
return AddOwner<TOwner, TValue>(source);
}
public static StyledProperty<TValue> AddOwner<TOwner, TValue>(
string _, // for compatibility with WinUI/UWP DependencyPropertyHelper
StyledProperty<TValue> source,
Action<TOwner, TValue, TValue> changed) where TOwner : AvaloniaObject
{
return AddOwner(source, changed);
}
public static void SetBinding(this AvaloniaObject target, AvaloniaProperty property, Binding binding)
{
target.Bind(property, binding);

View file

@ -78,8 +78,8 @@ namespace MapControl
static MapBase()
{
BackgroundProperty.OverrideDefaultValue(typeof(MapBase), Brushes.White);
ClipToBoundsProperty.OverrideDefaultValue(typeof(MapBase), true);
BackgroundProperty.OverrideDefaultValue<MapBase>(Brushes.White);
ClipToBoundsProperty.OverrideDefaultValue<MapBase>(true);
Animation.RegisterCustomAnimator<Location, LocationAnimator>();
}

View file

@ -3,40 +3,4 @@ using Avalonia.Controls;
namespace MapControl
{
/// <summary>
/// ContentControl placed on a MapPanel at a geographic location specified by the Location property.
/// </summary>
public class MapContentControl : ContentControl
{
public static readonly StyledProperty<Location> LocationProperty =
DependencyPropertyHelper.AddOwner<MapContentControl, Location>(MapPanel.LocationProperty);
public static readonly StyledProperty<bool> AutoCollapseProperty =
DependencyPropertyHelper.AddOwner<MapContentControl, bool>(MapPanel.AutoCollapseProperty);
/// <summary>
/// Gets/sets MapPanel.Location.
/// </summary>
public Location Location
{
get => GetValue(LocationProperty);
set => SetValue(LocationProperty, value);
}
/// <summary>
/// Gets/sets MapPanel.AutoCollapse.
/// </summary>
public bool AutoCollapse
{
get => GetValue(AutoCollapseProperty);
set => SetValue(AutoCollapseProperty, value);
}
}
/// <summary>
/// MapContentControl with a Pushpin Style.
/// </summary>
public class Pushpin : MapContentControl
{
}
}

View file

@ -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<Location> LocationProperty =
DependencyPropertyHelper.AddOwner<MapItem, Location>(MapPanel.LocationProperty,
(item, oldValue, newValue) => item.UpdateMapTransform());
public static readonly StyledProperty<bool> AutoCollapseProperty =
DependencyPropertyHelper.AddOwner<MapItem, bool>(MapPanel.AutoCollapseProperty);
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
if (e.Pointer.Type != PointerType.Mouse &&

View file

@ -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
{
/// <summary>
/// ContentControl placed on a MapPanel at a geographic location specified by the Location property.
/// </summary>
public partial class MapContentControl : ContentControl
{
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.AddOwner<MapContentControl, Location>(
nameof(Location), MapPanel.LocationProperty);
public static readonly DependencyProperty AutoCollapseProperty =
DependencyPropertyHelper.AddOwner<MapContentControl, bool>(
nameof(AutoCollapse), MapPanel.AutoCollapseProperty);
/// <summary>
/// Gets/sets MapPanel.Location.
/// </summary>
public Location Location
{
get => (Location)GetValue(LocationProperty);
set => SetValue(LocationProperty, value);
}
/// <summary>
/// Gets/sets MapPanel.AutoCollapse.
/// </summary>
public bool AutoCollapse
{
get => (bool)GetValue(AutoCollapseProperty);
set => SetValue(AutoCollapseProperty, value);
}
}
/// <summary>
/// MapContentControl with a Pushpin Style.
/// </summary>
public partial class Pushpin : MapContentControl
{
}
}

View file

@ -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
/// </summary>
public partial class MapItem : ListBoxItem, IMapElement
{
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.AddOwner<MapItem, Location>(
nameof(Location), MapPanel.LocationProperty, (item, _, _) => item.UpdateMapTransform());
public static readonly DependencyProperty AutoCollapseProperty =
DependencyPropertyHelper.AddOwner<MapItem, bool>(
nameof(AutoCollapse), MapPanel.AutoCollapseProperty);
/// <summary>
/// Gets/sets MapPanel.Location.
/// </summary>

View file

@ -104,10 +104,25 @@ namespace MapControl
public static DependencyProperty AddOwner<TOwner, TValue>(
DependencyProperty source,
Action<TOwner, TValue, TValue> changed = null) where TOwner : DependencyObject
Action<TOwner, TValue, TValue> 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<TOwner, TValue>(
string _, // for compatibility with WinUI/UWP DependencyPropertyHelper
DependencyProperty source) where TOwner : DependencyObject
{
return AddOwner<TOwner, TValue>(source);
}
public static DependencyProperty AddOwner<TOwner, TValue>(
string _, // for compatibility with WinUI/UWP DependencyPropertyHelper
DependencyProperty source,
Action<TOwner, TValue, TValue> changed) where TOwner : DependencyObject
{
return AddOwner(source, changed);
}
}
}

View file

@ -1,47 +1,16 @@
using System.Windows;
using System.Windows.Controls;
namespace MapControl
{
/// <summary>
/// ContentControl placed on a MapPanel at a geographic location specified by the Location property.
/// </summary>
public class MapContentControl : ContentControl
public partial class MapContentControl
{
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.AddOwner<MapContentControl, Location>(MapPanel.LocationProperty);
public static readonly DependencyProperty AutoCollapseProperty =
DependencyPropertyHelper.AddOwner<MapContentControl, bool>(MapPanel.AutoCollapseProperty);
/// <summary>
/// Gets/sets MapPanel.Location.
/// </summary>
public Location Location
{
get => (Location)GetValue(LocationProperty);
set => SetValue(LocationProperty, value);
}
/// <summary>
/// Gets/sets MapPanel.AutoCollapse.
/// </summary>
public bool AutoCollapse
{
get => (bool)GetValue(AutoCollapseProperty);
set => SetValue(AutoCollapseProperty, value);
}
static MapContentControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MapContentControl), new FrameworkPropertyMetadata(typeof(MapContentControl)));
}
}
/// <summary>
/// MapContentControl with a Pushpin Style.
/// </summary>
public class Pushpin : MapContentControl
public partial class Pushpin
{
static Pushpin()
{

View file

@ -6,13 +6,6 @@ namespace MapControl
{
public partial class MapItem
{
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.AddOwner<MapItem, Location>(MapPanel.LocationProperty,
(item, oldValue, newValue) => item.UpdateMapTransform());
public static readonly DependencyProperty AutoCollapseProperty =
DependencyPropertyHelper.AddOwner<MapItem, bool>(MapPanel.AutoCollapseProperty);
static MapItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MapItem), new FrameworkPropertyMetadata(typeof(MapItem)));

View file

@ -40,5 +40,20 @@ namespace MapControl
return DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), metadata);
}
public static DependencyProperty AddOwner<TOwner, TValue>(
string name,
DependencyProperty source,
Action<TOwner, TValue, TValue> 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);
}
}
}

View file

@ -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<MapContentControl, Location>(nameof(Location), null,
(control, oldValue, newValue) => MapPanel.SetLocation(control, newValue));
public static readonly DependencyProperty AutoCollapseProperty =
DependencyPropertyHelper.Register<MapContentControl, bool>(nameof(AutoCollapse), false,
(control, oldValue, newValue) => MapPanel.SetAutoCollapse(control, newValue));
/// <summary>
/// Gets/sets MapPanel.Location.
/// </summary>
public Location Location
{
get => (Location)GetValue(LocationProperty);
set => SetValue(LocationProperty, value);
}
/// <summary>
/// Gets/sets MapPanel.AutoCollapse.
/// </summary>
public bool AutoCollapse
{
get => (bool)GetValue(AutoCollapseProperty);
set => SetValue(AutoCollapseProperty, value);
}
public MapContentControl()
{
DefaultStyleKey = typeof(MapContentControl);
@ -73,10 +45,7 @@ namespace MapControl
}
}
/// <summary>
/// MapContentControl with a Pushpin Style.
/// </summary>
public partial class Pushpin : MapContentControl
public partial class Pushpin
{
public Pushpin()
{

View file

@ -16,18 +16,6 @@ namespace MapControl
{
public partial class MapItem
{
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.Register<MapItem, Location>(nameof(Location), null,
(item, oldValue, newValue) =>
{
MapPanel.SetLocation(item, newValue);
item.UpdateMapTransform();
});
public static readonly DependencyProperty AutoCollapseProperty =
DependencyPropertyHelper.Register<MapItem, bool>(nameof(AutoCollapse), false,
(item, oldValue, newValue) => MapPanel.SetAutoCollapse(item, newValue));
private Windows.Foundation.Point? pointerPressedPosition;
public MapItem()