XAML-Map-Control/MapControl/WPF/MapPath.WPF.cs

78 lines
2.4 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2022-01-14 20:22:56 +01:00
// © 2022 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
namespace MapControl
{
public partial class MapPath : Shape, IWeakEventListener
{
2022-11-02 23:51:15 +01:00
public static readonly DependencyProperty DataProperty = Path.DataProperty.AddOwner(
typeof(MapPath), new PropertyMetadata(null,
(o, e) =>
{
if (e.NewValue != e.OldValue) // Data is actually a new Geometry
{
((MapPath)o).UpdateData();
}
}));
public Geometry Data
{
2022-08-06 10:40:59 +02:00
get => (Geometry)GetValue(DataProperty);
set => SetValue(DataProperty, value);
}
2022-08-06 10:40:59 +02:00
protected override Geometry DefiningGeometry => Data;
#region Methods used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon
protected void DataCollectionPropertyChanged(DependencyPropertyChangedEventArgs e)
{
2020-04-16 23:15:03 +02:00
if (e.OldValue is INotifyCollectionChanged oldCollection)
{
2020-04-16 23:15:03 +02:00
CollectionChangedEventManager.RemoveListener(oldCollection, this);
}
2020-04-16 23:15:03 +02:00
if (e.NewValue is INotifyCollectionChanged newCollection)
{
2020-04-16 23:15:03 +02:00
CollectionChangedEventManager.AddListener(newCollection, this);
}
UpdateData();
}
2020-05-13 18:17:28 +02:00
bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
UpdateData();
return true;
}
protected void AddPolylineLocations(PathFigureCollection pathFigures, IEnumerable<Location> locations, double longitudeOffset, bool closed)
{
2020-05-13 18:17:28 +02:00
if (locations.Count() >= 2)
{
2020-05-13 18:17:28 +02:00
var points = locations.Select(location => LocationToView(location, longitudeOffset));
var figure = new PathFigure
{
StartPoint = points.First(),
IsClosed = closed,
IsFilled = closed
};
figure.Segments.Add(new PolyLineSegment(points.Skip(1), true));
2020-05-13 18:17:28 +02:00
pathFigures.Add(figure);
}
}
2020-05-13 18:17:28 +02:00
#endregion
}
}