XAML-Map-Control/MapControl/MapPolyline.cs
2015-01-20 17:52:02 +01:00

75 lines
2.5 KiB
C#

// XAML Map Control - http://xamlmapcontrol.codeplex.com/
// © 2015 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.Collections.Generic;
using System.Collections.Specialized;
#if WINDOWS_RUNTIME
using Windows.UI.Xaml;
#else
using System.Windows;
using System.ComponentModel;
#endif
namespace MapControl
{
/// <summary>
/// A polyline or polygon created from a collection of Locations.
/// </summary>
public partial class MapPolyline : MapPath
{
public static readonly DependencyProperty LocationsProperty = DependencyProperty.Register(
"Locations", typeof(IEnumerable<Location>), typeof(MapPolyline),
new PropertyMetadata(null, LocationsPropertyChanged));
public static readonly DependencyProperty IsClosedProperty = DependencyProperty.Register(
"IsClosed", typeof(bool), typeof(MapPolyline),
new PropertyMetadata(false, (o, e) => ((MapPolyline)o).UpdateData()));
/// <summary>
/// Gets or sets the locations that define the polyline points.
/// </summary>
#if !WINDOWS_RUNTIME
[TypeConverter(typeof(LocationCollectionConverter))]
#endif
public IEnumerable<Location> Locations
{
get { return (IEnumerable<Location>)GetValue(LocationsProperty); }
set { SetValue(LocationsProperty, value); }
}
/// <summary>
/// Gets or sets a value that indicates if the polyline is closed, i.e. is a polygon.
/// </summary>
public bool IsClosed
{
get { return (bool)GetValue(IsClosedProperty); }
set { SetValue(IsClosedProperty, value); }
}
private void LocationCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateData();
}
private static void LocationsPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var mapPolyline = (MapPolyline)obj;
var oldCollection = e.OldValue as INotifyCollectionChanged;
var newCollection = e.NewValue as INotifyCollectionChanged;
if (oldCollection != null)
{
oldCollection.CollectionChanged -= mapPolyline.LocationCollectionChanged;
}
if (newCollection != null)
{
newCollection.CollectionChanged += mapPolyline.LocationCollectionChanged;
}
mapPolyline.UpdateData();
}
}
}