Updated MapContentControl, MapItem, MapPath

This commit is contained in:
ClemensFischer 2026-02-02 12:10:09 +01:00
parent 2dc7431c25
commit 7cfb80520b
18 changed files with 216 additions and 133 deletions

View file

@ -56,6 +56,29 @@ namespace MapControl
return property;
}
public static StyledProperty<TValue> AddOwner<TOwner, TValue>(
StyledProperty<TValue> source) where TOwner : AvaloniaObject
{
return source.AddOwner<TOwner>();
}
public static StyledProperty<TValue> AddOwner<TOwner, TValue>(
StyledProperty<TValue> source,
TValue defaultValue) where TOwner : AvaloniaObject
{
return source.AddOwner<TOwner>(new StyledPropertyMetadata<TValue>(new Optional<TValue>(defaultValue)));
}
public static StyledProperty<TValue> AddOwner<TOwner, TValue>(
StyledProperty<TValue> source,
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 void SetBinding(this AvaloniaObject target, AvaloniaProperty property, Binding binding)
{
target.Bind(property, binding);

View file

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

View file

@ -0,0 +1,42 @@
using Avalonia;
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

@ -15,13 +15,13 @@ namespace MapControl
}
public static readonly StyledProperty<IBrush> ForegroundProperty =
TextElement.ForegroundProperty.AddOwner<MapGraticule>();
DependencyPropertyHelper.AddOwner<MapGraticule, IBrush>(TextElement.ForegroundProperty);
public static readonly StyledProperty<FontFamily> FontFamilyProperty =
TextElement.FontFamilyProperty.AddOwner<MapGraticule>();
DependencyPropertyHelper.AddOwner<MapGraticule, FontFamily>(TextElement.FontFamilyProperty);
public static readonly StyledProperty<double> FontSizeProperty =
TextElement.FontSizeProperty.AddOwner<MapGraticule>(new StyledPropertyMetadata<double>(12d));
DependencyPropertyHelper.AddOwner<MapGraticule, double>(TextElement.FontSizeProperty, 12d);
/// <summary>
/// Implements IMapElement.ParentMap.

View file

@ -1,10 +1,18 @@
using Avalonia.Controls;
using Avalonia;
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

@ -6,12 +6,9 @@ namespace MapControl
{
public partial class MapPath : Shape
{
public static readonly StyledProperty<Geometry> DataProperty = Path.DataProperty.AddOwner<MapPath>();
static MapPath()
{
DataProperty.Changed.AddClassHandler<MapPath, Geometry>((path, e) => path.UpdateData());
}
public static readonly StyledProperty<Geometry> DataProperty =
DependencyPropertyHelper.AddOwner<MapPath, Geometry>(Path.DataProperty,
(path, oldValue, newValue) => path.UpdateData());
public Geometry Data
{
@ -20,17 +17,5 @@ namespace MapControl
}
protected override Geometry CreateDefiningGeometry() => Data;
private void SetDataTransform(Matrix matrix)
{
if (Data.Transform is MatrixTransform transform)
{
transform.Matrix = matrix;
}
else
{
Data.Transform = new MatrixTransform(matrix);
}
}
}
}

View file

@ -1,54 +0,0 @@
#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.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);
}
}
/// <summary>
/// MapContentControl with a Pushpin Style.
/// </summary>
public partial class Pushpin : MapContentControl
{
}
}

View file

@ -1,13 +1,10 @@
#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
@ -22,18 +19,6 @@ namespace MapControl
/// </summary>
public partial class MapItem : ListBoxItem, IMapElement
{
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));
/// <summary>
/// Gets/sets MapPanel.Location.
/// </summary>

View file

@ -1,11 +1,15 @@
#if WPF
using System.Windows;
using System.Windows.Media;
#elif UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
#elif AVALONIA
using Avalonia;
using Avalonia.Media;
#endif
namespace MapControl
@ -62,6 +66,22 @@ namespace MapControl
UpdateData();
}
protected void SetDataTransform(Matrix matrix)
{
if (Data.Transform is MatrixTransform transform
#if WPF
&& !transform.IsFrozen
#endif
)
{
transform.Matrix = matrix;
}
else
{
Data.Transform = new MatrixTransform { Matrix = matrix };
}
}
protected virtual void UpdateData()
{
if (Data != null && ParentMap != null && Location != null)

View file

@ -88,5 +88,26 @@ namespace MapControl
{
return DependencyProperty.RegisterReadOnly(name, typeof(TValue), typeof(TOwner), new PropertyMetadata(defaultValue));
}
public static DependencyProperty AddOwner<TOwner, TValue>(
DependencyProperty source) where TOwner : DependencyObject
{
return source.AddOwner(typeof(TOwner));
}
public static DependencyProperty AddOwner<TOwner, TValue>(
DependencyProperty source,
TValue defaultValue) where TOwner : DependencyObject
{
return source.AddOwner(typeof(TOwner), new FrameworkPropertyMetadata(defaultValue));
}
public static DependencyProperty AddOwner<TOwner, TValue>(
DependencyProperty source,
Action<TOwner, TValue, TValue> changed = null) where TOwner : DependencyObject
{
return source.AddOwner(typeof(TOwner), new FrameworkPropertyMetadata(
(o, e) => changed((TOwner)o, (TValue)e.OldValue, (TValue)e.NewValue)));
}
}
}

View file

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

View file

@ -1,16 +1,47 @@
using System.Windows;
using System.Windows.Controls;
namespace MapControl
{
public partial class MapContentControl
/// <summary>
/// ContentControl placed on a MapPanel at a geographic location specified by the Location property.
/// </summary>
public class MapContentControl : ContentControl
{
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)));
}
}
public partial class Pushpin
/// <summary>
/// MapContentControl with a Pushpin Style.
/// </summary>
public class Pushpin : MapContentControl
{
static Pushpin()
{

View file

@ -9,13 +9,13 @@ namespace MapControl
public partial class MapGraticule : FrameworkElement, IMapElement
{
public static readonly DependencyProperty ForegroundProperty =
TextElement.ForegroundProperty.AddOwner(typeof(MapGraticule));
DependencyPropertyHelper.AddOwner<MapGraticule, Brush>(TextElement.ForegroundProperty);
public static readonly DependencyProperty FontFamilyProperty =
TextElement.FontFamilyProperty.AddOwner(typeof(MapGraticule));
DependencyPropertyHelper.AddOwner<MapGraticule, FontFamily>(TextElement.FontFamilyProperty);
public static readonly DependencyProperty FontSizeProperty =
TextElement.FontSizeProperty.AddOwner(typeof(MapGraticule), new FrameworkPropertyMetadata(12d));
DependencyPropertyHelper.AddOwner<MapGraticule, double>(TextElement.FontSizeProperty, 12d);
/// <summary>
/// Implements IMapElement.ParentMap.

View file

@ -6,6 +6,13 @@ 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

@ -7,8 +7,8 @@ namespace MapControl
public partial class MapPath : Shape
{
public static readonly DependencyProperty DataProperty =
Path.DataProperty.AddOwner(typeof(MapPath),
new FrameworkPropertyMetadata(null, (o, e) => ((MapPath)o).DataPropertyChanged(e)));
DependencyPropertyHelper.AddOwner<MapPath, Geometry>(Path.DataProperty,
(path, oldValue, newValue) => path.DataPropertyChanged(oldValue, newValue));
public Geometry Data
{
@ -18,29 +18,15 @@ namespace MapControl
protected override Geometry DefiningGeometry => Data;
protected void SetDataTransform(Matrix matrix)
{
if (Data.Transform is MatrixTransform transform && !transform.IsFrozen)
{
transform.Matrix = matrix;
}
else
{
Data.Transform = new MatrixTransform(matrix);
}
}
private void DataPropertyChanged(DependencyPropertyChangedEventArgs e)
private void DataPropertyChanged(Geometry oldValue, Geometry newValue)
{
// Check if Data is actually a new Geometry.
//
if (e.NewValue != null && !ReferenceEquals(e.NewValue, e.OldValue))
if (newValue != null && !ReferenceEquals(newValue, oldValue))
{
var data = (Geometry)e.NewValue;
if (data.IsFrozen)
if (newValue.IsFrozen)
{
Data = data.Clone(); // DataPropertyChanged called again
Data = newValue.Clone(); // DataPropertyChanged called again
}
else
{

View file

@ -1,15 +1,43 @@
#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
public partial class MapContentControl : ContentControl
{
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);
@ -45,6 +73,9 @@ namespace MapControl
}
}
/// <summary>
/// MapContentControl with a Pushpin Style.
/// </summary>
public partial class Pushpin : MapContentControl
{
public Pushpin()

View file

@ -16,6 +16,18 @@ 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()

View file

@ -1,8 +1,6 @@
#if UWP
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
#else
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
#endif
@ -14,17 +12,5 @@ namespace MapControl
{
MapPanel.InitMapElement(this);
}
private void SetDataTransform(Matrix matrix)
{
if (Data.Transform is MatrixTransform transform)
{
transform.Matrix = matrix;
}
else
{
Data.Transform = new MatrixTransform { Matrix = matrix };
}
}
}
}