Version 4.9.0: Reworked MapPanel child arrangement.

This commit is contained in:
ClemensF 2018-04-30 23:13:50 +02:00
parent 0a7d0fccd2
commit 56d376c4d0
24 changed files with 326 additions and 322 deletions

View file

@ -7,8 +7,8 @@ using System.Runtime.InteropServices;
[assembly: AssemblyCompany("Clemens Fischer")] [assembly: AssemblyCompany("Clemens Fischer")]
[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyCopyright("© 2018 Clemens Fischer")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("4.8.0")] [assembly: AssemblyVersion("4.9.0")]
[assembly: AssemblyFileVersion("4.8.0")] [assembly: AssemblyFileVersion("4.9.0")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
[assembly: ComVisible(false)] [assembly: ComVisible(false)]

View file

@ -7,8 +7,8 @@ using System.Runtime.InteropServices;
[assembly: AssemblyCompany("Clemens Fischer")] [assembly: AssemblyCompany("Clemens Fischer")]
[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyCopyright("© 2018 Clemens Fischer")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("4.8.0")] [assembly: AssemblyVersion("4.9.0")]
[assembly: AssemblyFileVersion("4.8.0")] [assembly: AssemblyFileVersion("4.9.0")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
[assembly: ComVisible(false)] [assembly: ComVisible(false)]

View file

@ -7,8 +7,8 @@ using System.Runtime.InteropServices;
[assembly: AssemblyCompany("Clemens Fischer")] [assembly: AssemblyCompany("Clemens Fischer")]
[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyCopyright("© 2018 Clemens Fischer")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("4.8.0")] [assembly: AssemblyVersion("4.9.0")]
[assembly: AssemblyFileVersion("4.8.0")] [assembly: AssemblyFileVersion("4.9.0")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
[assembly: ComVisible(false)] [assembly: ComVisible(false)]

View file

@ -7,8 +7,8 @@ using System.Runtime.InteropServices;
[assembly: AssemblyCompany("Clemens Fischer")] [assembly: AssemblyCompany("Clemens Fischer")]
[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyCopyright("© 2018 Clemens Fischer")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("4.8.0")] [assembly: AssemblyVersion("4.9.0")]
[assembly: AssemblyFileVersion("4.8.0")] [assembly: AssemblyFileVersion("4.9.0")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
[assembly: ComVisible(false)] [assembly: ComVisible(false)]

View file

@ -62,12 +62,12 @@ namespace MapControl
public static readonly DependencyProperty InlinesSourceProperty = DependencyProperty.RegisterAttached( public static readonly DependencyProperty InlinesSourceProperty = DependencyProperty.RegisterAttached(
"InlinesSource", typeof(string), typeof(HyperlinkText), new PropertyMetadata(null, InlinesSourcePropertyChanged)); "InlinesSource", typeof(string), typeof(HyperlinkText), new PropertyMetadata(null, InlinesSourcePropertyChanged));
public static string GetInlinesSource(UIElement element) public static string GetInlinesSource(DependencyObject element)
{ {
return (string)element.GetValue(InlinesSourceProperty); return (string)element.GetValue(InlinesSourceProperty);
} }
public static void SetInlinesSource(UIElement element, string value) public static void SetInlinesSource(DependencyObject element, string value)
{ {
element.SetValue(InlinesSourceProperty, value); element.SetValue(InlinesSourceProperty, value);
} }

View file

@ -4,6 +4,7 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
#if WINDOWS_UWP #if WINDOWS_UWP
using Windows.Foundation; using Windows.Foundation;
@ -295,7 +296,7 @@ namespace MapControl
boundingBox.West += offset; boundingBox.West += offset;
boundingBox.East += offset; boundingBox.East += offset;
foreach (UIElement element in Children) foreach (var element in Children.OfType<FrameworkElement>())
{ {
var bbox = GetBoundingBox(element); var bbox = GetBoundingBox(element);
@ -309,7 +310,7 @@ namespace MapControl
private void ClearImages() private void ClearImages()
{ {
foreach (UIElement element in Children) foreach (var element in Children.OfType<FrameworkElement>())
{ {
element.ClearValue(BoundingBoxProperty); element.ClearValue(BoundingBoxProperty);
element.ClearValue(Image.SourceProperty); element.ClearValue(Image.SourceProperty);

View file

@ -24,6 +24,16 @@ namespace MapControl
{ {
Loaded += (s, e) => Loaded += (s, e) =>
{ {
#if WINDOWS_UWP
if (Foreground == null)
{
SetBinding(ForegroundProperty, new Binding
{
Source = this,
Path = new PropertyPath("ParentMap.Foreground")
});
}
#endif
if (Stroke == null) if (Stroke == null)
{ {
SetBinding(StrokeProperty, new Binding SetBinding(StrokeProperty, new Binding

View file

@ -3,6 +3,7 @@
// Licensed under the Microsoft Public License (Ms-PL) // Licensed under the Microsoft Public License (Ms-PL)
using System; using System;
using System.Linq;
#if WINDOWS_UWP #if WINDOWS_UWP
using Windows.Foundation; using Windows.Foundation;
using Windows.UI.Xaml; using Windows.UI.Xaml;
@ -18,7 +19,6 @@ namespace MapControl
{ {
/// <summary> /// <summary>
/// Optional interface to hold the value of the MapPanel.ParentMap attached property. /// Optional interface to hold the value of the MapPanel.ParentMap attached property.
/// May be used to get notified when the property value changes.
/// </summary> /// </summary>
public interface IMapElement public interface IMapElement
{ {
@ -28,8 +28,6 @@ namespace MapControl
/// <summary> /// <summary>
/// Arranges child elements on a Map at positions specified by the attached property Location, /// Arranges child elements on a Map at positions specified by the attached property Location,
/// or in rectangles specified by the attached property BoundingBox. /// or in rectangles specified by the attached property BoundingBox.
/// An element's viewport position is assigned as TranslateTransform to its RenderTransform property,
/// either directly or as last child of a TransformGroup.
/// </summary> /// </summary>
public partial class MapPanel : Panel, IMapElement public partial class MapPanel : Panel, IMapElement
{ {
@ -46,37 +44,57 @@ namespace MapControl
InitMapElement(this); InitMapElement(this);
} }
public static Location GetLocation(UIElement element) public static Location GetLocation(FrameworkElement element)
{ {
return (Location)element.GetValue(LocationProperty); return (Location)element.GetValue(LocationProperty);
} }
public static void SetLocation(UIElement element, Location value) public static void SetLocation(FrameworkElement element, Location value)
{ {
element.SetValue(LocationProperty, value); element.SetValue(LocationProperty, value);
} }
public static BoundingBox GetBoundingBox(UIElement element) public static BoundingBox GetBoundingBox(FrameworkElement element)
{ {
return (BoundingBox)element.GetValue(BoundingBoxProperty); return (BoundingBox)element.GetValue(BoundingBoxProperty);
} }
public static void SetBoundingBox(UIElement element, BoundingBox value) public static void SetBoundingBox(FrameworkElement element, BoundingBox value)
{ {
element.SetValue(BoundingBoxProperty, value); element.SetValue(BoundingBoxProperty, value);
} }
public static Point? GetViewportPosition(FrameworkElement element)
{
return (Point?)element.GetValue(ViewportPositionProperty);
}
public MapBase ParentMap public MapBase ParentMap
{ {
get { return parentMap; } get { return parentMap; }
set { SetParentMap(value); } set
{
if (parentMap != null && parentMap != this)
{
parentMap.ViewportChanged -= OnViewportChanged;
}
parentMap = value;
if (parentMap != null && parentMap != this)
{
parentMap.ViewportChanged += OnViewportChanged;
OnViewportChanged(new ViewportChangedEventArgs());
}
}
} }
protected override Size MeasureOverride(Size availableSize) protected override Size MeasureOverride(Size availableSize)
{ {
availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity); availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
foreach (UIElement element in Children) foreach (var element in Children.OfType<FrameworkElement>())
{ {
element.Measure(availableSize); element.Measure(availableSize);
} }
@ -86,62 +104,34 @@ namespace MapControl
protected override Size ArrangeOverride(Size finalSize) protected override Size ArrangeOverride(Size finalSize)
{ {
foreach (UIElement element in Children) foreach (var element in Children.OfType<FrameworkElement>())
{ {
BoundingBox boundingBox;
Location location; Location location;
BoundingBox boundingBox;
Point? viewportPosition = null;
if ((boundingBox = GetBoundingBox(element)) != null) if ((location = GetLocation(element)) != null)
{ {
ArrangeElementWithBoundingBox(element); viewportPosition = ArrangeElementWithLocation(element, parentMap, location);
SetBoundingBoxRect(element, parentMap, boundingBox);
} }
else if ((location = GetLocation(element)) != null) else if ((boundingBox = GetBoundingBox(element)) != null)
{ {
ArrangeElementWithLocation(element); ArrangeElementWithBoundingBox(element, parentMap, boundingBox);
SetViewportPosition(element, parentMap, location);
} }
else else
{ {
ArrangeElement(element, finalSize); ArrangeElement(element, finalSize);
} }
SetViewportPosition(element, viewportPosition);
} }
return finalSize; return finalSize;
} }
protected virtual void SetParentMap(MapBase map)
{
if (parentMap != null && parentMap != this)
{
parentMap.ViewportChanged -= OnViewportChanged;
}
parentMap = map;
if (parentMap != null && parentMap != this)
{
parentMap.ViewportChanged += OnViewportChanged;
OnViewportChanged(new ViewportChangedEventArgs());
}
}
protected virtual void OnViewportChanged(ViewportChangedEventArgs e) protected virtual void OnViewportChanged(ViewportChangedEventArgs e)
{ {
foreach (UIElement element in Children) InvalidateArrange();
{
BoundingBox boundingBox;
Location location;
if ((boundingBox = GetBoundingBox(element)) != null)
{
SetBoundingBoxRect(element, parentMap, boundingBox);
}
else if ((location = GetLocation(element)) != null)
{
SetViewportPosition(element, parentMap, location);
}
}
} }
private void OnViewportChanged(object sender, ViewportChangedEventArgs e) private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
@ -161,245 +151,201 @@ namespace MapControl
private static void LocationPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) private static void LocationPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{ {
var element = (UIElement)obj; var element = (FrameworkElement)obj;
var map = GetParentMap(element); var parentMap = GetParentMap(element);
var location = (Location)e.NewValue; var location = (Location)e.NewValue;
Point? viewportPosition = null;
if (location == null) if (location != null)
{ {
ArrangeElement(element, map?.RenderSize ?? new Size()); viewportPosition = ArrangeElementWithLocation(element, parentMap, location);
} }
else if (e.OldValue == null) else
{ {
ArrangeElementWithLocation(element); // arrange once when Location was null before ArrangeElement(element, parentMap?.RenderSize ?? new Size());
} }
SetViewportPosition(element, map, location); SetViewportPosition(element, viewportPosition);
} }
private static void BoundingBoxPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) private static void BoundingBoxPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{ {
var element = (FrameworkElement)obj; var element = (FrameworkElement)obj;
var map = GetParentMap(element); var parentMap = GetParentMap(element);
var boundingBox = (BoundingBox)e.NewValue; var boundingBox = (BoundingBox)e.NewValue;
if (boundingBox == null) if (boundingBox != null)
{ {
ArrangeElement(element, map?.RenderSize ?? new Size()); ArrangeElementWithBoundingBox(element, parentMap, boundingBox);
} }
else if (e.OldValue == null) else
{ {
ArrangeElementWithBoundingBox(element); // arrange once when BoundingBox was null before ArrangeElement(element, parentMap?.RenderSize ?? new Size());
} }
SetBoundingBoxRect(element, map, boundingBox); SetViewportPosition(element, null);
} }
private static void SetViewportPosition(UIElement element, MapBase parentMap, Location location) private static void ArrangeElement(FrameworkElement element, Size parentSize)
{ {
var viewportPosition = new Point(); var rect = new Rect(new Point(), element.DesiredSize);
if (parentMap != null && location != null) switch (element.HorizontalAlignment)
{ {
viewportPosition = parentMap.MapProjection.LocationToViewportPoint(location); case HorizontalAlignment.Center:
rect.X = (parentSize.Width - rect.Width) / 2d;
break;
case HorizontalAlignment.Right:
rect.X = parentSize.Width - rect.Width;
break;
case HorizontalAlignment.Stretch:
rect.Width = parentSize.Width;
break;
default:
break;
}
switch (element.VerticalAlignment)
{
case VerticalAlignment.Center:
rect.Y = (parentSize.Height - rect.Height) / 2d;
break;
case VerticalAlignment.Bottom:
rect.Y = parentSize.Height - rect.Height;
break;
case VerticalAlignment.Stretch:
rect.Height = parentSize.Height;
break;
default:
break;
}
if (element.UseLayoutRounding)
{
rect.X = Math.Round(rect.X);
rect.Y = Math.Round(rect.Y);
}
element.Arrange(rect);
}
private static Point ArrangeElementWithLocation(FrameworkElement element, MapBase parentMap, Location location)
{
var pos = new Point();
var rect = new Rect(pos, element.DesiredSize);
if (parentMap != null)
{
pos = parentMap.MapProjection.LocationToViewportPoint(location);
if (parentMap.MapProjection.IsContinuous && if (parentMap.MapProjection.IsContinuous &&
(viewportPosition.X < 0d || viewportPosition.X > parentMap.RenderSize.Width || (pos.X < 0d || pos.X > parentMap.RenderSize.Width ||
viewportPosition.Y < 0d || viewportPosition.Y > parentMap.RenderSize.Height)) pos.Y < 0d || pos.Y > parentMap.RenderSize.Height))
{ {
viewportPosition = parentMap.MapProjection.LocationToViewportPoint(new Location( pos = parentMap.MapProjection.LocationToViewportPoint(new Location(
location.Latitude, location.Latitude,
Location.NearestLongitude(location.Longitude, parentMap.Center.Longitude))); Location.NearestLongitude(location.Longitude, parentMap.Center.Longitude)));
} }
if ((bool)element.GetValue(UseLayoutRoundingProperty)) rect.X = pos.X;
{ rect.Y = pos.Y;
viewportPosition.X = Math.Round(viewportPosition.X);
viewportPosition.Y = Math.Round(viewportPosition.Y);
}
} }
var translateTransform = element.RenderTransform as TranslateTransform; switch (element.HorizontalAlignment)
if (translateTransform == null)
{ {
var transformGroup = element.RenderTransform as TransformGroup; case HorizontalAlignment.Center:
rect.X -= rect.Width / 2d;
break;
if (transformGroup == null) case HorizontalAlignment.Right:
{ rect.X -= rect.Width;
translateTransform = new TranslateTransform(); break;
element.RenderTransform = translateTransform;
}
else
{
if (transformGroup.Children.Count > 0)
{
translateTransform = transformGroup.Children[transformGroup.Children.Count - 1] as TranslateTransform;
}
if (translateTransform == null) default:
{ break;
translateTransform = new TranslateTransform();
transformGroup.Children.Add(translateTransform);
}
}
} }
translateTransform.X = viewportPosition.X; switch (element.VerticalAlignment)
translateTransform.Y = viewportPosition.Y; {
case VerticalAlignment.Center:
rect.Y -= rect.Height / 2d;
break;
case VerticalAlignment.Bottom:
rect.Y -= rect.Height;
break;
default:
break;
}
if (element.UseLayoutRounding)
{
rect.X = Math.Round(rect.X);
rect.Y = Math.Round(rect.Y);
}
element.Arrange(rect);
return pos;
} }
private static void SetBoundingBoxRect(UIElement element, MapBase parentMap, BoundingBox boundingBox) private static void ArrangeElementWithBoundingBox(FrameworkElement element, MapBase parentMap, BoundingBox boundingBox)
{ {
var rect = new Rect();
var rotation = 0d; var rotation = 0d;
var viewportPosition = new Point();
if (parentMap != null && boundingBox != null) if (parentMap != null)
{ {
var projection = parentMap.MapProjection; var projection = parentMap.MapProjection;
var rect = projection.BoundingBoxToRect(boundingBox); rect = projection.BoundingBoxToRect(boundingBox);
var center = new Point(rect.X + rect.Width / 2d, rect.Y + rect.Height / 2d);
rotation = parentMap.Heading; var center = new Point(rect.X + rect.Width / 2d, rect.Y + rect.Height / 2d);
viewportPosition = projection.ViewportTransform.Transform(center); var pos = projection.ViewportTransform.Transform(center);
if (parentMap.MapProjection.IsContinuous && if (parentMap.MapProjection.IsContinuous &&
(viewportPosition.X < 0d || viewportPosition.X > parentMap.RenderSize.Width || (pos.X < 0d || pos.X > parentMap.RenderSize.Width ||
viewportPosition.Y < 0d || viewportPosition.Y > parentMap.RenderSize.Height)) pos.Y < 0d || pos.Y > parentMap.RenderSize.Height))
{ {
var location = projection.PointToLocation(center); var location = projection.PointToLocation(center);
location.Longitude = Location.NearestLongitude(location.Longitude, parentMap.Center.Longitude); location.Longitude = Location.NearestLongitude(location.Longitude, parentMap.Center.Longitude);
viewportPosition = projection.LocationToViewportPoint(location); pos = projection.LocationToViewportPoint(location);
} }
var width = rect.Width * projection.ViewportScale; rect.Width *= projection.ViewportScale;
var height = rect.Height * projection.ViewportScale; rect.Height *= projection.ViewportScale;
rect.X = pos.X - rect.Width / 2d;
rect.Y = pos.Y - rect.Height / 2d;
if (element is FrameworkElement) if (element.UseLayoutRounding)
{ {
((FrameworkElement)element).Width = width; rect.X = Math.Round(rect.X);
((FrameworkElement)element).Height = height; rect.Y = Math.Round(rect.Y);
}
else
{
element.Arrange(new Rect(-width / 2d, -height / 2d, width, height)); // ???
} }
rotation = parentMap.Heading;
} }
var transformGroup = element.RenderTransform as TransformGroup; element.Width = rect.Width;
RotateTransform rotateTransform; element.Height = rect.Height;
TranslateTransform translateTransform; element.Arrange(rect);
if (transformGroup == null || var rotateTransform = element.RenderTransform as RotateTransform;
transformGroup.Children.Count != 2 ||
(rotateTransform = transformGroup.Children[0] as RotateTransform) == null || if (rotateTransform == null)
(translateTransform = transformGroup.Children[1] as TranslateTransform) == null)
{ {
transformGroup = new TransformGroup();
rotateTransform = new RotateTransform(); rotateTransform = new RotateTransform();
translateTransform = new TranslateTransform(); element.RenderTransform = rotateTransform;
transformGroup.Children.Add(rotateTransform);
transformGroup.Children.Add(translateTransform);
element.RenderTransform = transformGroup;
element.RenderTransformOrigin = new Point(0.5, 0.5); element.RenderTransformOrigin = new Point(0.5, 0.5);
} }
rotateTransform.Angle = rotation; rotateTransform.Angle = rotation;
translateTransform.X = viewportPosition.X;
translateTransform.Y = viewportPosition.Y;
}
private static void ArrangeElementWithBoundingBox(UIElement element)
{
var size = element.DesiredSize;
element.Arrange(new Rect(-size.Width / 2d, -size.Height / 2d, size.Width, size.Height));
}
private static void ArrangeElementWithLocation(UIElement element)
{
var rect = new Rect(new Point(), element.DesiredSize);
if (element is FrameworkElement)
{
switch (((FrameworkElement)element).HorizontalAlignment)
{
case HorizontalAlignment.Center:
rect.X = -rect.Width / 2d;
break;
case HorizontalAlignment.Right:
rect.X = -rect.Width;
break;
default:
break;
}
switch (((FrameworkElement)element).VerticalAlignment)
{
case VerticalAlignment.Center:
rect.Y = -rect.Height / 2d;
break;
case VerticalAlignment.Bottom:
rect.Y = -rect.Height;
break;
default:
break;
}
}
element.Arrange(rect);
}
private static void ArrangeElement(UIElement element, Size parentSize)
{
var rect = new Rect(new Point(), element.DesiredSize);
if (element is FrameworkElement)
{
switch (((FrameworkElement)element).HorizontalAlignment)
{
case HorizontalAlignment.Center:
rect.X = (parentSize.Width - rect.Width) / 2d;
break;
case HorizontalAlignment.Right:
rect.X = parentSize.Width - rect.Width;
break;
case HorizontalAlignment.Stretch:
rect.Width = parentSize.Width;
break;
default:
break;
}
switch (((FrameworkElement)element).VerticalAlignment)
{
case VerticalAlignment.Center:
rect.Y = (parentSize.Height - rect.Height) / 2d;
break;
case VerticalAlignment.Bottom:
rect.Y = parentSize.Height - rect.Height;
break;
case VerticalAlignment.Stretch:
rect.Height = parentSize.Height;
break;
default:
break;
}
}
element.Arrange(rect);
} }
} }
} }

View file

@ -26,7 +26,7 @@ namespace MapControl
public class MapScale : MapOverlay public class MapScale : MapOverlay
{ {
public static readonly DependencyProperty PaddingProperty = DependencyProperty.Register( public static readonly DependencyProperty PaddingProperty = DependencyProperty.Register(
nameof(Padding), typeof(Thickness), typeof(MapScale), null); nameof(Padding), typeof(Thickness), typeof(MapScale), new PropertyMetadata(new Thickness(4)));
private TextBlock label = new TextBlock(); private TextBlock label = new TextBlock();
private Polyline line = new Polyline(); private Polyline line = new Polyline();
@ -34,11 +34,11 @@ namespace MapControl
public MapScale() public MapScale()
{ {
IsHitTestVisible = false; IsHitTestVisible = false;
#if WINDOWS_UWP
MinWidth = 100d; MinWidth = 100d;
Padding = new Thickness(4d); #else
HorizontalAlignment = HorizontalAlignment.Left; SetCurrentValue(MinWidthProperty, 100d);
VerticalAlignment = VerticalAlignment.Bottom; #endif
label.HorizontalAlignment = HorizontalAlignment.Left; label.HorizontalAlignment = HorizontalAlignment.Left;
label.VerticalAlignment = VerticalAlignment.Top; label.VerticalAlignment = VerticalAlignment.Top;
label.TextAlignment = TextAlignment.Center; label.TextAlignment = TextAlignment.Center;
@ -49,16 +49,12 @@ namespace MapControl
Path = new PropertyPath("Foreground") Path = new PropertyPath("Foreground")
}); });
line.SetBinding(Shape.StrokeProperty, new Binding line.SetBinding(Shape.StrokeProperty, new Binding
{ {
Source = this, Source = this,
Path = new PropertyPath("Stroke") Path = new PropertyPath("Stroke")
}); });
line.SetBinding(Shape.StrokeThicknessProperty, new Binding line.SetBinding(Shape.StrokeThicknessProperty, new Binding
{ {
Source = this, Source = this,

View file

@ -220,9 +220,9 @@ namespace MapControl
{ {
availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity); availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
foreach (UIElement element in Children) foreach (var tile in Tiles)
{ {
element.Measure(availableSize); tile.Image.Measure(availableSize);
} }
return new Size(); return new Size();

View file

@ -35,18 +35,7 @@ namespace MapControl
Data = new PathGeometry() Data = new PathGeometry()
}; };
path.SetBinding(Shape.StrokeProperty, new Binding SetStrokeBindings(path);
{
Source = this,
Path = new PropertyPath("Stroke")
});
path.SetBinding(Shape.StrokeThicknessProperty, new Binding
{
Source = this,
Path = new PropertyPath("StrokeThickness")
});
Children.Add(path); Children.Add(path);
} }

View file

@ -6,6 +6,7 @@ using Windows.UI.Text;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
namespace MapControl namespace MapControl
{ {
@ -56,18 +57,61 @@ namespace MapControl
public static readonly DependencyProperty StrokeMiterLimitProperty = DependencyProperty.Register( public static readonly DependencyProperty StrokeMiterLimitProperty = DependencyProperty.Register(
nameof(StrokeMiterLimit), typeof(double), typeof(MapOverlay), new PropertyMetadata(1d)); nameof(StrokeMiterLimit), typeof(double), typeof(MapOverlay), new PropertyMetadata(1d));
protected override void SetParentMap(MapBase parentMap) public void SetStrokeBindings(Shape shape)
{ {
if (parentMap != null && Foreground == null) shape.SetBinding(Shape.StrokeProperty, new Binding
{ {
SetBinding(ForegroundProperty, new Binding Source = this,
{ Path = new PropertyPath("Stroke")
Source = parentMap, });
Path = new PropertyPath("Foreground")
});
}
base.SetParentMap(parentMap); shape.SetBinding(Shape.StrokeThicknessProperty, new Binding
{
Source = this,
Path = new PropertyPath("StrokeThickness")
});
shape.SetBinding(Shape.StrokeDashArrayProperty, new Binding
{
Source = this,
Path = new PropertyPath("StrokeDashArray")
});
shape.SetBinding(Shape.StrokeDashOffsetProperty, new Binding
{
Source = this,
Path = new PropertyPath("StrokeDashOffset")
});
shape.SetBinding(Shape.StrokeDashCapProperty, new Binding
{
Source = this,
Path = new PropertyPath("StrokeDashCap")
});
shape.SetBinding(Shape.StrokeStartLineCapProperty, new Binding
{
Source = this,
Path = new PropertyPath("StrokeStartLineCap")
});
shape.SetBinding(Shape.StrokeEndLineCapProperty, new Binding
{
Source = this,
Path = new PropertyPath("StrokeEndLineCap")
});
shape.SetBinding(Shape.StrokeLineJoinProperty, new Binding
{
Source = this,
Path = new PropertyPath("StrokeLineJoin")
});
shape.SetBinding(Shape.StrokeMiterLimitProperty, new Binding
{
Source = this,
Path = new PropertyPath("StrokeMiterLimit")
});
} }
} }
} }

View file

@ -12,6 +12,9 @@ namespace MapControl
public static readonly DependencyProperty ParentMapProperty = DependencyProperty.RegisterAttached( public static readonly DependencyProperty ParentMapProperty = DependencyProperty.RegisterAttached(
"ParentMap", typeof(MapBase), typeof(MapPanel), new PropertyMetadata(null, ParentMapPropertyChanged)); "ParentMap", typeof(MapBase), typeof(MapPanel), new PropertyMetadata(null, ParentMapPropertyChanged));
private static readonly DependencyProperty ViewportPositionProperty = DependencyProperty.RegisterAttached(
"ViewportPosition", typeof(Point?), typeof(MapPanel), new PropertyMetadata(null));
public static void InitMapElement(FrameworkElement element) public static void InitMapElement(FrameworkElement element)
{ {
if (element is MapBase) if (element is MapBase)
@ -28,7 +31,7 @@ namespace MapControl
} }
} }
public static MapBase GetParentMap(UIElement element) public static MapBase GetParentMap(FrameworkElement element)
{ {
var parentMap = (MapBase)element.GetValue(ParentMapProperty); var parentMap = (MapBase)element.GetValue(ParentMapProperty);
@ -40,14 +43,19 @@ namespace MapControl
return parentMap; return parentMap;
} }
private static MapBase FindParentMap(UIElement element) private static MapBase FindParentMap(FrameworkElement element)
{ {
var parent = VisualTreeHelper.GetParent(element) as UIElement; var parent = VisualTreeHelper.GetParent(element) as FrameworkElement;
return parent == null ? null return parent == null ? null
: ((parent as MapBase) : ((parent as MapBase)
?? (MapBase)element.GetValue(ParentMapProperty) ?? (MapBase)element.GetValue(ParentMapProperty)
?? FindParentMap(parent)); ?? FindParentMap(parent));
} }
private static void SetViewportPosition(FrameworkElement element, Point? viewportPosition)
{
element.SetValue(ViewportPositionProperty, viewportPosition);
}
} }
} }

View file

@ -7,8 +7,8 @@ using System.Runtime.InteropServices;
[assembly: AssemblyCompany("Clemens Fischer")] [assembly: AssemblyCompany("Clemens Fischer")]
[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyCopyright("© 2018 Clemens Fischer")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("4.8.0")] [assembly: AssemblyVersion("4.9.0")]
[assembly: AssemblyFileVersion("4.8.0")] [assembly: AssemblyFileVersion("4.9.0")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
[assembly: ComVisible(false)] [assembly: ComVisible(false)]

View file

@ -66,17 +66,7 @@ namespace MapControl
var latLabels = new List<Label>((int)((boundingBox.North - latLabelStart) / lineDistance) + 1); var latLabels = new List<Label>((int)((boundingBox.North - latLabelStart) / lineDistance) + 1);
var lonLabels = new List<Label>((int)((boundingBox.East - lonLabelStart) / lineDistance) + 1); var lonLabels = new List<Label>((int)((boundingBox.East - lonLabelStart) / lineDistance) + 1);
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch); var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
var pen = new Pen var pen = CreatePen();
{
Brush = Stroke,
Thickness = StrokeThickness,
LineJoin = StrokeLineJoin,
MiterLimit = StrokeMiterLimit,
StartLineCap = StrokeStartLineCap,
EndLineCap = StrokeEndLineCap,
DashCap = StrokeDashCap,
DashStyle = new DashStyle(StrokeDashArray, StrokeDashOffset)
};
for (var lat = latLabelStart; lat <= boundingBox.North; lat += lineDistance) for (var lat = latLabelStart; lat <= boundingBox.North; lat += lineDistance)
{ {

View file

@ -4,6 +4,7 @@
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes; using System.Windows.Shapes;
namespace MapControl namespace MapControl
@ -11,42 +12,52 @@ namespace MapControl
public partial class MapOverlay public partial class MapOverlay
{ {
public static readonly DependencyProperty FontSizeProperty = Control.FontSizeProperty.AddOwner(typeof(MapOverlay)); public static readonly DependencyProperty FontSizeProperty = Control.FontSizeProperty.AddOwner(typeof(MapOverlay));
public static readonly DependencyProperty FontFamilyProperty = Control.FontFamilyProperty.AddOwner(typeof(MapOverlay)); public static readonly DependencyProperty FontFamilyProperty = Control.FontFamilyProperty.AddOwner(typeof(MapOverlay));
public static readonly DependencyProperty FontStyleProperty = Control.FontStyleProperty.AddOwner(typeof(MapOverlay)); public static readonly DependencyProperty FontStyleProperty = Control.FontStyleProperty.AddOwner(typeof(MapOverlay));
public static readonly DependencyProperty FontStretchProperty = Control.FontStretchProperty.AddOwner(typeof(MapOverlay)); public static readonly DependencyProperty FontStretchProperty = Control.FontStretchProperty.AddOwner(typeof(MapOverlay));
public static readonly DependencyProperty FontWeightProperty = Control.FontWeightProperty.AddOwner(typeof(MapOverlay)); public static readonly DependencyProperty FontWeightProperty = Control.FontWeightProperty.AddOwner(typeof(MapOverlay));
public static readonly DependencyProperty ForegroundProperty = Control.ForegroundProperty.AddOwner(typeof(MapOverlay)); public static readonly DependencyProperty ForegroundProperty = Control.ForegroundProperty.AddOwner(typeof(MapOverlay));
public static readonly DependencyProperty StrokeProperty = Shape.StrokeProperty.AddOwner( public static readonly DependencyProperty StrokeProperty = Shape.StrokeProperty.AddOwner(typeof(MapOverlay),
typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty StrokeThicknessProperty = Shape.StrokeThicknessProperty.AddOwner( public static readonly DependencyProperty StrokeThicknessProperty = Shape.StrokeThicknessProperty.AddOwner(typeof(MapOverlay),
typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); new FrameworkPropertyMetadata(1d, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty StrokeDashArrayProperty = Shape.StrokeDashArrayProperty.AddOwner( public static readonly DependencyProperty StrokeDashArrayProperty = Shape.StrokeDashArrayProperty.AddOwner(typeof(MapOverlay),
typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty StrokeDashOffsetProperty = Shape.StrokeDashOffsetProperty.AddOwner( public static readonly DependencyProperty StrokeDashOffsetProperty = Shape.StrokeDashOffsetProperty.AddOwner(typeof(MapOverlay),
typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty StrokeDashCapProperty = Shape.StrokeDashCapProperty.AddOwner( public static readonly DependencyProperty StrokeDashCapProperty = Shape.StrokeDashCapProperty.AddOwner(typeof(MapOverlay),
typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); new FrameworkPropertyMetadata(PenLineCap.Flat, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty StrokeStartLineCapProperty = Shape.StrokeStartLineCapProperty.AddOwner( public static readonly DependencyProperty StrokeStartLineCapProperty = Shape.StrokeStartLineCapProperty.AddOwner(typeof(MapOverlay),
typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); new FrameworkPropertyMetadata(PenLineCap.Flat, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty StrokeEndLineCapProperty = Shape.StrokeEndLineCapProperty.AddOwner( public static readonly DependencyProperty StrokeEndLineCapProperty = Shape.StrokeEndLineCapProperty.AddOwner(typeof(MapOverlay),
typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); new FrameworkPropertyMetadata(PenLineCap.Flat, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty StrokeLineJoinProperty = Shape.StrokeLineJoinProperty.AddOwner( public static readonly DependencyProperty StrokeLineJoinProperty = Shape.StrokeLineJoinProperty.AddOwner(typeof(MapOverlay),
typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); new FrameworkPropertyMetadata(PenLineJoin.Miter, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty StrokeMiterLimitProperty = Shape.StrokeMiterLimitProperty.AddOwner( public static readonly DependencyProperty StrokeMiterLimitProperty = Shape.StrokeMiterLimitProperty.AddOwner(typeof(MapOverlay),
typeof(MapOverlay), new FrameworkPropertyMetadata { AffectsRender = true }); new FrameworkPropertyMetadata(1d, FrameworkPropertyMetadataOptions.AffectsRender));
public Pen CreatePen()
{
return new Pen
{
Brush = Stroke,
Thickness = StrokeThickness,
LineJoin = StrokeLineJoin,
MiterLimit = StrokeMiterLimit,
StartLineCap = StrokeStartLineCap,
EndLineCap = StrokeEndLineCap,
DashCap = StrokeDashCap,
DashStyle = new DashStyle(StrokeDashArray, StrokeDashOffset)
};
}
} }
} }

View file

@ -12,9 +12,13 @@ namespace MapControl
"ParentMap", typeof(MapBase), typeof(MapPanel), new FrameworkPropertyMetadata( "ParentMap", typeof(MapBase), typeof(MapPanel), new FrameworkPropertyMetadata(
null, FrameworkPropertyMetadataOptions.Inherits, ParentMapPropertyChanged)); null, FrameworkPropertyMetadataOptions.Inherits, ParentMapPropertyChanged));
public static readonly DependencyProperty ParentMapProperty = ParentMapPropertyKey.DependencyProperty; private static readonly DependencyPropertyKey ViewportPositionPropertyKey = DependencyProperty.RegisterAttachedReadOnly(
"ViewportPosition", typeof(Point?), typeof(MapPanel), new PropertyMetadata());
public static MapBase GetParentMap(UIElement element) public static readonly DependencyProperty ParentMapProperty = ParentMapPropertyKey.DependencyProperty;
public static readonly DependencyProperty ViewportPositionProperty = ViewportPositionPropertyKey.DependencyProperty;
public static MapBase GetParentMap(FrameworkElement element)
{ {
return (MapBase)element.GetValue(ParentMapProperty); return (MapBase)element.GetValue(ParentMapProperty);
} }
@ -26,5 +30,10 @@ namespace MapControl
element.SetValue(ParentMapPropertyKey, element); element.SetValue(ParentMapPropertyKey, element);
} }
} }
private static void SetViewportPosition(FrameworkElement element, Point? viewportPosition)
{
element.SetValue(ViewportPositionPropertyKey, viewportPosition);
}
} }
} }

View file

@ -14,7 +14,7 @@ namespace MapControl
{ {
public abstract partial class MapShape : Shape, IWeakEventListener public abstract partial class MapShape : Shape, IWeakEventListener
{ {
protected Geometry Data { get; } public Geometry Data { get; }
protected override Geometry DefiningGeometry protected override Geometry DefiningGeometry
{ {

View file

@ -8,8 +8,8 @@ using System.Windows;
[assembly: AssemblyCompany("Clemens Fischer")] [assembly: AssemblyCompany("Clemens Fischer")]
[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyCopyright("© 2018 Clemens Fischer")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("4.8.0")] [assembly: AssemblyVersion("4.9.0")]
[assembly: AssemblyFileVersion("4.8.0")] [assembly: AssemblyFileVersion("4.9.0")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
[assembly: ComVisible(false)] [assembly: ComVisible(false)]

View file

@ -115,7 +115,7 @@
</Image> </Image>
<map:MapGraticule x:Name="mapGraticule" Opacity="0.6"/> <map:MapGraticule x:Name="mapGraticule" Opacity="0.6"/>
<map:MapScale/> <map:MapScale HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
<!-- use ItemTemplate or ItemContainerStyle alternatively --> <!-- use ItemTemplate or ItemContainerStyle alternatively -->
<map:MapItemsControl ItemsSource="{Binding Polylines}" <map:MapItemsControl ItemsSource="{Binding Polylines}"

View file

@ -8,7 +8,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyCopyright("© 2018 Clemens Fischer")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("4.7.0")] [assembly: AssemblyVersion("4.9.0")]
[assembly: AssemblyFileVersion("4.7.0")] [assembly: AssemblyFileVersion("4.9.0")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: ComVisible(false)] [assembly: ComVisible(false)]

View file

@ -2,7 +2,6 @@
using System.Globalization; using System.Globalization;
using System.Windows; using System.Windows;
using System.Windows.Data; using System.Windows.Data;
using System.Windows.Media;
using MapControl; using MapControl;
namespace WpfApplication namespace WpfApplication
@ -13,13 +12,14 @@ namespace WpfApplication
{ {
var visibility = Visibility.Hidden; var visibility = Visibility.Hidden;
if (values.Length == 2 && values[0] is Map && values[1] is Transform) if (values.Length == 2 && values[0] is MapBase && values[1] is Point?)
{ {
var parentMap = (Map)values[0]; var parentMap = (MapBase)values[0];
var transform = ((Transform)values[1]).Value; var position = (Point?)values[1];
if (transform.OffsetX >= 0d && transform.OffsetX <= parentMap.ActualWidth && if (position.HasValue &&
transform.OffsetY >= 0d && transform.OffsetY <= parentMap.ActualHeight) position.Value.X >= 0d && position.Value.X <= parentMap.ActualWidth &&
position.Value.Y >= 0d && position.Value.Y <= parentMap.ActualHeight)
{ {
visibility = Visibility.Visible; visibility = Visibility.Visible;
} }

View file

@ -88,7 +88,7 @@
<Setter.Value> <Setter.Value>
<MultiBinding Converter="{StaticResource LocationToVisibilityConverter}"> <MultiBinding Converter="{StaticResource LocationToVisibilityConverter}">
<Binding Path="(map:MapPanel.ParentMap)" RelativeSource="{RelativeSource Self}"/> <Binding Path="(map:MapPanel.ParentMap)" RelativeSource="{RelativeSource Self}"/>
<Binding Path="RenderTransform" RelativeSource="{RelativeSource Self}"/> <Binding Path="(map:MapPanel.ViewportPosition)" RelativeSource="{RelativeSource Self}"/>
</MultiBinding> </MultiBinding>
</Setter.Value> </Setter.Value>
</Setter> </Setter>
@ -139,7 +139,7 @@
map:MapPanel.BoundingBox="53.54031,8.08594,53.74871,8.43750"/> map:MapPanel.BoundingBox="53.54031,8.08594,53.74871,8.43750"/>
<map:MapGraticule x:Name="mapGraticule" Opacity="0.6"/> <map:MapGraticule x:Name="mapGraticule" Opacity="0.6"/>
<map:MapScale/> <map:MapScale HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
<!-- use ItemTemplate or ItemContainerStyle alternatively --> <!-- use ItemTemplate or ItemContainerStyle alternatively -->
<map:MapItemsControl ItemsSource="{Binding Polylines}" <map:MapItemsControl ItemsSource="{Binding Polylines}"
@ -171,7 +171,7 @@
<map:Pushpin.Visibility> <map:Pushpin.Visibility>
<MultiBinding Converter="{StaticResource LocationToVisibilityConverter}"> <MultiBinding Converter="{StaticResource LocationToVisibilityConverter}">
<Binding Path="(map:MapPanel.ParentMap)" RelativeSource="{RelativeSource Self}"/> <Binding Path="(map:MapPanel.ParentMap)" RelativeSource="{RelativeSource Self}"/>
<Binding Path="RenderTransform" RelativeSource="{RelativeSource Self}"/> <Binding Path="(map:MapPanel.ViewportPosition)" RelativeSource="{RelativeSource Self}"/>
</MultiBinding> </MultiBinding>
</map:Pushpin.Visibility> </map:Pushpin.Visibility>
</map:Pushpin> </map:Pushpin>

View file

@ -7,8 +7,8 @@ using System.Runtime.InteropServices;
[assembly: AssemblyCompany("Clemens Fischer")] [assembly: AssemblyCompany("Clemens Fischer")]
[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyCopyright("© 2018 Clemens Fischer")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("4.7.0")] [assembly: AssemblyVersion("4.9.0")]
[assembly: AssemblyFileVersion("4.7.0")] [assembly: AssemblyFileVersion("4.9.0")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
[assembly: ComVisible(false)] [assembly: ComVisible(false)]