Removed DependencyPropertyHelper.AddOwner

This commit is contained in:
ClemensFischer 2025-06-10 15:25:17 +02:00
parent a516d12391
commit d944058ab3
15 changed files with 72 additions and 125 deletions

View file

@ -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<TValue> AddOwner<TOwner, TValue>(
StyledProperty<TValue> property,
TValue defaultValue = default,
Action<TOwner, TValue, TValue> changed = null)
where TOwner : AvaloniaObject
{
var newProperty = property.AddOwner<TOwner>();
if (!Equals(defaultValue, property.GetMetadata(typeof(TOwner)).DefaultValue))
{
newProperty.OverrideMetadata<TOwner>(new StyledPropertyMetadata<TValue>(defaultValue));
}
if (changed != null)
{
newProperty.Changed.AddClassHandler<TOwner, TValue>((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);

View file

@ -31,7 +31,7 @@ namespace MapControl
public partial class MapBase
{
public static readonly StyledProperty<Brush> ForegroundProperty =
DependencyPropertyHelper.AddOwner<MapBase, Brush>(TextElement.ForegroundProperty, Brushes.Black);
TextElement.ForegroundProperty.AddOwner<MapBase>(new StyledPropertyMetadata<Brush>(new Optional<Brush>(Brushes.Black)));
public static readonly StyledProperty<Easing> AnimationEasingProperty =
DependencyPropertyHelper.Register<MapBase, Easing>(nameof(AnimationEasing), new QuadraticEaseOut());

View file

@ -6,10 +6,10 @@
public class MapContentControl : ContentControl
{
public static readonly StyledProperty<bool> AutoCollapseProperty =
DependencyPropertyHelper.AddOwner<MapContentControl, bool>(MapPanel.AutoCollapseProperty);
MapPanel.AutoCollapseProperty.AddOwner<MapContentControl>();
public static readonly StyledProperty<Location> LocationProperty =
DependencyPropertyHelper.AddOwner<MapContentControl, Location>(MapPanel.LocationProperty);
MapPanel.LocationProperty.AddOwner<MapContentControl>();
/// <summary>
/// Gets/sets MapPanel.AutoCollapse.

View file

@ -12,31 +12,13 @@ namespace MapControl
}
public static readonly StyledProperty<IBrush> ForegroundProperty =
DependencyPropertyHelper.AddOwner<MapGraticule, IBrush>(TextElement.ForegroundProperty);
TextElement.ForegroundProperty.AddOwner<MapGraticule>();
public static readonly StyledProperty<FontFamily> FontFamilyProperty =
DependencyPropertyHelper.AddOwner<MapGraticule, FontFamily>(TextElement.FontFamilyProperty);
TextElement.FontFamilyProperty.AddOwner<MapGraticule>();
public static readonly StyledProperty<double> FontSizeProperty =
DependencyPropertyHelper.AddOwner<MapGraticule, double>(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<MapGraticule>(new StyledPropertyMetadata<double>(12d));
private MapBase parentMap;

View file

@ -3,11 +3,15 @@
public partial class MapItem
{
public static readonly StyledProperty<bool> AutoCollapseProperty =
DependencyPropertyHelper.AddOwner<MapItem, bool>(MapPanel.AutoCollapseProperty);
MapPanel.AutoCollapseProperty.AddOwner<MapItem>();
public static readonly StyledProperty<Location> LocationProperty =
DependencyPropertyHelper.AddOwner<MapItem, Location>(MapPanel.LocationProperty, null,
(item, oldValue, newValue) => item.UpdateMapTransform());
MapPanel.LocationProperty.AddOwner<MapItem>();
static MapItem()
{
LocationProperty.Changed.AddClassHandler<MapItem, Location>((item, e) => item.UpdateMapTransform());
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{

View file

@ -4,14 +4,12 @@ namespace MapControl
{
public partial class MapPath : Shape
{
public MapPath()
{
Stretch = Stretch.None;
}
public static readonly StyledProperty<Geometry> DataProperty = Path.DataProperty.AddOwner<MapPath>();
public static readonly StyledProperty<Geometry> DataProperty =
DependencyPropertyHelper.AddOwner<MapPath, Geometry>(Path.DataProperty, null,
(path, oldValue, newValue) => path.UpdateData());
static MapPath()
{
DataProperty.Changed.AddClassHandler<MapPath, Geometry>((path, e) => path.UpdateData());
}
public Geometry Data
{

View file

@ -40,17 +40,11 @@ namespace MapControl
private const double LineInterpolationResolution = 2d;
public static readonly DependencyProperty StrokeThicknessProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(StrokeThickness), 0.5);
public static readonly DependencyProperty MinLineDistanceProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(MinLineDistance), 150d);
public double StrokeThickness
{
get => (double)GetValue(StrokeThicknessProperty);
set => SetValue(StrokeThicknessProperty, value);
}
public static readonly DependencyProperty StrokeThicknessProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(StrokeThickness), 0.5);
/// <summary>
/// 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;

View file

@ -88,26 +88,5 @@ namespace MapControl
{
return DependencyProperty.RegisterReadOnly(name, typeof(TValue), typeof(TOwner), new PropertyMetadata(defaultValue));
}
public static DependencyProperty AddOwner<TOwner, TValue>(
DependencyProperty property,
TValue defaultValue = default,
Action<TOwner, TValue, TValue> 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);
}
}
}

View file

@ -8,7 +8,7 @@ namespace MapControl
public partial class MapBase
{
public static readonly DependencyProperty ForegroundProperty =
DependencyPropertyHelper.AddOwner<MapBase, Brush>(TextElement.ForegroundProperty, Brushes.Black);
TextElement.ForegroundProperty.AddOwner(typeof(MapBase), new FrameworkPropertyMetadata(Brushes.Black));
public static readonly DependencyProperty AnimationEasingFunctionProperty =
DependencyPropertyHelper.Register<MapBase, IEasingFunction>(nameof(AnimationEasingFunction),

View file

@ -9,10 +9,10 @@ namespace MapControl
public class MapContentControl : ContentControl
{
public static readonly DependencyProperty AutoCollapseProperty =
DependencyPropertyHelper.AddOwner<MapContentControl, bool>(MapPanel.AutoCollapseProperty);
MapPanel.AutoCollapseProperty.AddOwner(typeof(MapContentControl));
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.AddOwner<MapContentControl, Location>(MapPanel.LocationProperty);
MapPanel.LocationProperty.AddOwner(typeof(MapContentControl));
static MapContentControl()
{

View file

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

View file

@ -7,11 +7,11 @@ namespace MapControl
public partial class MapItem
{
public static readonly DependencyProperty AutoCollapseProperty =
DependencyPropertyHelper.AddOwner<MapItem, bool>(MapPanel.AutoCollapseProperty);
MapPanel.AutoCollapseProperty.AddOwner(typeof(MapItem));
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.AddOwner<MapItem, Location>(MapPanel.LocationProperty, null,
(item, oldValue, newValue) => item.UpdateMapTransform());
MapPanel.LocationProperty.AddOwner(typeof(MapItem),
new FrameworkPropertyMetadata(null, (o, e) => ((MapItem)o).UpdateMapTransform()));
static MapItem()
{

View file

@ -6,14 +6,9 @@ namespace MapControl
{
public partial class MapPath : Shape
{
public MapPath()
{
Stretch = Stretch.None;
}
public static readonly DependencyProperty DataProperty =
DependencyPropertyHelper.AddOwner<MapPath, Geometry>(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
{

View file

@ -28,24 +28,6 @@ namespace MapControl
public static readonly DependencyProperty FontSizeProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(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)

View file

@ -12,7 +12,6 @@ namespace MapControl
{
public MapPath()
{
Stretch = Stretch.None;
MapPanel.InitMapElement(this);
}