XAML-Map-Control/MapControl/MapPolyline.cs

240 lines
9.2 KiB
C#
Raw Normal View History

2012-05-04 12:52:20 +02:00
// WPF MapControl - http://wpfmapcontrol.codeplex.com/
// Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2012-10-25 08:42:51 +02:00
using System.Globalization;
2012-05-04 12:52:20 +02:00
using System.Linq;
2012-04-25 22:02:53 +02:00
using System.Windows;
2012-10-25 08:42:51 +02:00
using System.Windows.Data;
2012-04-25 22:02:53 +02:00
using System.Windows.Media;
using System.Windows.Shapes;
namespace MapControl
{
2012-05-04 12:52:20 +02:00
/// <summary>
/// An open map polygon, defined by a collection of geographic locations in the Locations property.
/// </summary>
2012-10-25 08:42:51 +02:00
public class MapPolyline : FrameworkElement
2012-04-25 22:02:53 +02:00
{
public static readonly DependencyProperty StrokeProperty = Shape.StrokeProperty.AddOwner(
2012-10-25 08:42:51 +02:00
typeof(MapPolyline), new FrameworkPropertyMetadata((o, e) => ((MapPolyline)o).Drawing.Pen.Brush = (Brush)e.NewValue));
public static readonly DependencyProperty StrokeThicknessProperty = Shape.StrokeThicknessProperty.AddOwner(
typeof(MapPolyline));
2012-04-25 22:02:53 +02:00
public static readonly DependencyProperty StrokeDashArrayProperty = Shape.StrokeDashArrayProperty.AddOwner(
2012-10-25 08:42:51 +02:00
typeof(MapPolyline), new FrameworkPropertyMetadata((o, e) => ((MapPolyline)o).Drawing.Pen.DashStyle = new DashStyle((DoubleCollection)e.NewValue, ((MapPolyline)o).StrokeDashOffset)));
2012-04-25 22:02:53 +02:00
public static readonly DependencyProperty StrokeDashOffsetProperty = Shape.StrokeDashOffsetProperty.AddOwner(
2012-10-25 08:42:51 +02:00
typeof(MapPolyline), new FrameworkPropertyMetadata((o, e) => ((MapPolyline)o).Drawing.Pen.DashStyle = new DashStyle(((MapPolyline)o).StrokeDashArray, (double)e.NewValue)));
2012-04-25 22:02:53 +02:00
public static readonly DependencyProperty StrokeDashCapProperty = Shape.StrokeDashCapProperty.AddOwner(
2012-10-25 08:42:51 +02:00
typeof(MapPolyline), new FrameworkPropertyMetadata((o, e) => ((MapPolyline)o).Drawing.Pen.DashCap = (PenLineCap)e.NewValue));
2012-04-25 22:02:53 +02:00
public static readonly DependencyProperty StrokeStartLineCapProperty = Shape.StrokeStartLineCapProperty.AddOwner(
2012-10-25 08:42:51 +02:00
typeof(MapPolyline), new FrameworkPropertyMetadata((o, e) => ((MapPolyline)o).Drawing.Pen.StartLineCap = (PenLineCap)e.NewValue));
2012-04-25 22:02:53 +02:00
public static readonly DependencyProperty StrokeEndLineCapProperty = Shape.StrokeEndLineCapProperty.AddOwner(
2012-10-25 08:42:51 +02:00
typeof(MapPolyline), new FrameworkPropertyMetadata((o, e) => ((MapPolyline)o).Drawing.Pen.EndLineCap = (PenLineCap)e.NewValue));
2012-04-25 22:02:53 +02:00
public static readonly DependencyProperty StrokeLineJoinProperty = Shape.StrokeLineJoinProperty.AddOwner(
2012-10-25 08:42:51 +02:00
typeof(MapPolyline), new FrameworkPropertyMetadata((o, e) => ((MapPolyline)o).Drawing.Pen.LineJoin = (PenLineJoin)e.NewValue));
2012-04-25 22:02:53 +02:00
public static readonly DependencyProperty StrokeMiterLimitProperty = Shape.StrokeMiterLimitProperty.AddOwner(
2012-10-25 08:42:51 +02:00
typeof(MapPolyline), new FrameworkPropertyMetadata((o, e) => ((MapPolyline)o).Drawing.Pen.MiterLimit = (double)e.NewValue));
2012-04-25 22:02:53 +02:00
public static readonly DependencyProperty TransformStrokeProperty = DependencyProperty.Register(
2012-10-25 08:42:51 +02:00
"TransformStroke", typeof(bool), typeof(MapPolyline), new FrameworkPropertyMetadata((o, e) => ((MapPolyline)o).SetPenThicknessBinding()));
2012-05-04 12:52:20 +02:00
public static readonly DependencyProperty LocationsProperty = DependencyProperty.Register(
"Locations", typeof(LocationCollection), typeof(MapPolyline), new FrameworkPropertyMetadata((o, e) => ((MapPolyline)o).UpdateGeometry()));
2012-04-25 22:02:53 +02:00
2012-10-25 08:42:51 +02:00
protected readonly GeometryDrawing Drawing = new GeometryDrawing();
2012-04-25 22:02:53 +02:00
2012-10-25 08:42:51 +02:00
static MapPolyline()
2012-04-25 22:02:53 +02:00
{
2012-10-25 08:42:51 +02:00
MapPanel.ParentMapPropertyKey.OverrideMetadata(
typeof(MapPolyline),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, ParentMapPropertyChanged));
}
2012-04-25 22:02:53 +02:00
2012-10-25 08:42:51 +02:00
public MapPolyline()
{
Drawing.Pen = new Pen
2012-04-25 22:02:53 +02:00
{
2012-10-25 08:42:51 +02:00
Brush = Stroke,
Thickness = StrokeThickness,
DashStyle = new DashStyle(StrokeDashArray, StrokeDashOffset),
DashCap = StrokeDashCap,
StartLineCap = StrokeStartLineCap,
EndLineCap = StrokeEndLineCap,
LineJoin = StrokeLineJoin,
MiterLimit = StrokeMiterLimit
};
}
2012-04-25 22:02:53 +02:00
2012-10-25 08:42:51 +02:00
public MapBase ParentMap
{
get { return MapPanel.GetParentMap(this); }
2012-04-25 22:02:53 +02:00
}
public Brush Stroke
{
get { return (Brush)GetValue(StrokeProperty); }
set { SetValue(StrokeProperty, value); }
}
2012-10-25 08:42:51 +02:00
public double StrokeThickness
{
get { return (double)GetValue(StrokeThicknessProperty); }
set { SetValue(StrokeThicknessProperty, value); }
}
2012-04-25 22:02:53 +02:00
public DoubleCollection StrokeDashArray
{
get { return (DoubleCollection)GetValue(StrokeDashArrayProperty); }
set { SetValue(StrokeDashArrayProperty, value); }
}
public double StrokeDashOffset
{
get { return (double)GetValue(StrokeDashOffsetProperty); }
set { SetValue(StrokeDashOffsetProperty, value); }
}
public PenLineCap StrokeDashCap
{
get { return (PenLineCap)GetValue(StrokeDashCapProperty); }
set { SetValue(StrokeDashCapProperty, value); }
}
public PenLineCap StrokeStartLineCap
{
get { return (PenLineCap)GetValue(StrokeStartLineCapProperty); }
set { SetValue(StrokeStartLineCapProperty, value); }
}
public PenLineCap StrokeEndLineCap
{
get { return (PenLineCap)GetValue(StrokeEndLineCapProperty); }
set { SetValue(StrokeEndLineCapProperty, value); }
}
public PenLineJoin StrokeLineJoin
{
get { return (PenLineJoin)GetValue(StrokeLineJoinProperty); }
set { SetValue(StrokeLineJoinProperty, value); }
}
public double StrokeMiterLimit
{
get { return (double)GetValue(StrokeMiterLimitProperty); }
set { SetValue(StrokeMiterLimitProperty, value); }
}
public bool TransformStroke
{
get { return (bool)GetValue(TransformStrokeProperty); }
set { SetValue(TransformStrokeProperty, value); }
}
2012-05-04 12:52:20 +02:00
public LocationCollection Locations
{
get { return (LocationCollection)GetValue(LocationsProperty); }
set { SetValue(LocationsProperty, value); }
}
2012-04-25 22:02:53 +02:00
public PathGeometry TransformedGeometry
{
2012-10-25 08:42:51 +02:00
get { return Drawing.Geometry as PathGeometry; }
2012-04-25 22:02:53 +02:00
}
2012-10-25 08:42:51 +02:00
public double TransformedStrokeThickness
2012-04-25 22:02:53 +02:00
{
2012-10-25 08:42:51 +02:00
get { return Drawing.Pen.Thickness; }
2012-04-25 22:02:53 +02:00
}
2012-10-25 08:42:51 +02:00
protected override void OnRender(DrawingContext drawingContext)
2012-04-25 22:02:53 +02:00
{
2012-10-25 08:42:51 +02:00
drawingContext.DrawDrawing(Drawing);
2012-04-25 22:02:53 +02:00
}
2012-05-04 12:52:20 +02:00
protected virtual void UpdateGeometry()
{
UpdateGeometry(false);
}
protected void UpdateGeometry(bool closed)
2012-04-25 22:02:53 +02:00
{
2012-08-08 20:42:06 +02:00
if (ParentMap != null && Locations != null && Locations.Count > 0)
2012-04-25 22:02:53 +02:00
{
2012-10-25 08:42:51 +02:00
Drawing.Geometry = CreateGeometry(Locations, closed);
2012-04-25 22:02:53 +02:00
}
else
{
2012-10-25 08:42:51 +02:00
Drawing.Geometry = null;
2012-04-25 22:02:53 +02:00
}
}
2012-05-04 12:52:20 +02:00
2012-08-08 20:42:06 +02:00
private Geometry CreateGeometry(LocationCollection locations, bool closed)
2012-05-04 12:52:20 +02:00
{
StreamGeometry geometry = new StreamGeometry
{
2012-08-08 20:42:06 +02:00
Transform = ParentMap.ViewportTransform
2012-05-04 12:52:20 +02:00
};
using (StreamGeometryContext sgc = geometry.Open())
{
2012-08-08 20:42:06 +02:00
sgc.BeginFigure(ParentMap.MapTransform.Transform(locations.First()), closed, closed);
2012-05-04 12:52:20 +02:00
if (Locations.Count > 1)
{
2012-08-08 20:42:06 +02:00
sgc.PolyLineTo(ParentMap.MapTransform.Transform(locations.Skip(1)), true, true);
2012-05-04 12:52:20 +02:00
}
}
return geometry;
}
2012-10-25 08:42:51 +02:00
private void SetPenThicknessBinding()
{
BindingBase binding = new Binding { Source = this, Path = new PropertyPath(MapPolyline.StrokeThicknessProperty)};
if (TransformStroke && ParentMap != null)
{
MultiBinding multiBinding = new MultiBinding { Converter = new PenThicknessConverter() };
multiBinding.Bindings.Add(binding);
multiBinding.Bindings.Add(new Binding { Source = ParentMap, Path = new PropertyPath(MapBase.CenterScaleProperty)});
binding = multiBinding;
}
BindingOperations.SetBinding(Drawing.Pen, Pen.ThicknessProperty, binding);
}
private static void ParentMapPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
MapPolyline polyline = obj as MapPolyline;
if (polyline != null)
{
polyline.UpdateGeometry();
polyline.SetPenThicknessBinding();
}
}
private class PenThicknessConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return (double)values[0] * (double)values[1] * MapBase.MeterPerDegree;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
2012-04-25 22:02:53 +02:00
}
}