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

101 lines
3.4 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
namespace MapControl
{
2026-02-07 22:10:06 +01:00
public partial class MapGrid : FrameworkElement, IMapElement
{
public static readonly DependencyProperty ForegroundProperty =
2026-02-07 22:10:06 +01:00
DependencyPropertyHelper.AddOwner<MapGrid, Brush>(TextElement.ForegroundProperty);
public static readonly DependencyProperty FontFamilyProperty =
2026-02-07 22:10:06 +01:00
DependencyPropertyHelper.AddOwner<MapGrid, FontFamily>(TextElement.FontFamilyProperty);
public static readonly DependencyProperty FontSizeProperty =
2026-02-07 22:10:06 +01:00
DependencyPropertyHelper.AddOwner<MapGrid, double>(TextElement.FontSizeProperty, 12d);
2024-05-26 20:32:29 +02:00
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
public MapBase ParentMap
{
2025-12-27 21:24:01 +01:00
get;
2024-05-26 20:32:29 +02:00
set
{
2025-12-27 21:24:01 +01:00
if (field != null)
2024-05-26 20:32:29 +02:00
{
2025-12-27 21:24:01 +01:00
field.ViewportChanged -= OnViewportChanged;
2024-05-26 20:32:29 +02:00
}
2025-12-27 21:24:01 +01:00
field = value;
2024-05-26 20:32:29 +02:00
2025-12-27 21:24:01 +01:00
if (field != null)
2024-05-26 20:32:29 +02:00
{
2025-12-27 21:24:01 +01:00
field.ViewportChanged += OnViewportChanged;
2024-05-26 20:32:29 +02:00
}
}
}
2024-05-26 20:32:29 +02:00
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
2026-02-07 22:10:06 +01:00
{
OnViewportChanged(e);
}
protected virtual void OnViewportChanged(ViewportChangedEventArgs e)
{
InvalidateVisual();
}
protected override void OnRender(DrawingContext drawingContext)
{
2025-12-27 21:24:01 +01:00
if (ParentMap != null)
{
2022-04-27 18:00:40 +02:00
var pathGeometry = new PathGeometry();
2026-02-07 00:01:04 +01:00
var labels = new List<Label>();
2024-05-26 20:32:29 +02:00
var pen = new Pen
{
Brush = Foreground,
Thickness = StrokeThickness,
};
2026-02-07 22:10:06 +01:00
DrawGrid(pathGeometry.Figures, labels);
2026-02-07 00:01:04 +01:00
2024-05-26 20:32:29 +02:00
drawingContext.DrawGeometry(null, pen, pathGeometry);
2022-03-04 22:28:18 +01:00
2022-04-28 18:34:30 +02:00
if (labels.Count > 0)
2022-03-04 22:28:18 +01:00
{
var typeface = new Typeface(FontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
2022-04-28 18:34:30 +02:00
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
foreach (var label in labels)
{
2026-02-07 22:10:06 +01:00
var text = new FormattedText(label.Text,
2022-04-28 18:34:30 +02:00
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
var x = label.X + StrokeThickness / 2d + 2d;
2026-02-07 22:10:06 +01:00
var y = label.Y - text.Height / 2d;
if (label.Rotation != 0d)
{
drawingContext.PushTransform(new RotateTransform(label.Rotation, label.X, label.Y));
drawingContext.DrawText(text, new Point(x, y));
drawingContext.Pop();
}
else
{
drawingContext.DrawText(text, new Point(x, y));
}
2022-04-28 18:34:30 +02:00
}
2022-03-05 18:40:57 +01:00
}
2022-03-04 22:28:18 +01:00
}
}
2026-02-01 01:42:24 +01:00
private static PolyLineSegment CreatePolyLineSegment(IEnumerable<Point> points)
2022-03-04 22:28:18 +01:00
{
2026-02-01 01:42:24 +01:00
return new PolyLineSegment(points, true);
}
}
}