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

113 lines
4.3 KiB
C#
Raw Normal View History

2022-02-01 22:21:30 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2025-01-01 18:57:55 +01:00
// Copyright © Clemens Fischer
2022-02-01 22:21:30 +01:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MapControl
{
2022-02-05 15:52:17 +01:00
public partial class PushpinBorder : Decorator
2022-02-01 22:21:30 +01:00
{
2024-05-23 18:08:14 +02:00
public static readonly DependencyProperty ArrowSizeProperty =
DependencyPropertyHelper.Register<PushpinBorder, Size>(nameof(ArrowSize), new Size(10d, 20d),
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender);
public static readonly DependencyProperty BorderWidthProperty =
DependencyPropertyHelper.Register<PushpinBorder, double>(nameof(BorderWidth), 0d,
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender);
public static readonly DependencyProperty BackgroundProperty =
DependencyPropertyHelper.Register<PushpinBorder, Brush>(nameof(Background), null,
FrameworkPropertyMetadataOptions.AffectsRender);
public static readonly DependencyProperty BorderBrushProperty =
DependencyPropertyHelper.Register<PushpinBorder, Brush>(nameof(BorderBrush), null,
FrameworkPropertyMetadataOptions.AffectsRender);
public static readonly DependencyProperty CornerRadiusProperty =
DependencyPropertyHelper.Register<PushpinBorder, CornerRadius>(nameof(CornerRadius), new CornerRadius(),
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender);
public static readonly DependencyProperty PaddingProperty =
DependencyPropertyHelper.Register<PushpinBorder, Thickness>(nameof(Padding), new Thickness(2),
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender);
2022-02-01 22:21:30 +01:00
public Brush Background
{
2022-08-06 10:40:59 +02:00
get => (Brush)GetValue(BackgroundProperty);
set => SetValue(BackgroundProperty, value);
2022-02-01 22:21:30 +01:00
}
public Brush BorderBrush
{
2022-08-06 10:40:59 +02:00
get => (Brush)GetValue(BorderBrushProperty);
set => SetValue(BorderBrushProperty, value);
2022-02-01 22:21:30 +01:00
}
public CornerRadius CornerRadius
{
2022-08-06 10:40:59 +02:00
get => (CornerRadius)GetValue(CornerRadiusProperty);
set => SetValue(CornerRadiusProperty, value);
2022-02-01 22:21:30 +01:00
}
public Thickness Padding
{
2022-08-06 10:40:59 +02:00
get => (Thickness)GetValue(PaddingProperty);
set => SetValue(PaddingProperty, value);
2022-02-01 22:21:30 +01:00
}
protected override Size MeasureOverride(Size constraint)
{
2022-02-05 15:52:17 +01:00
var width = 2d * BorderWidth + Padding.Left + Padding.Right;
var height = 2d * BorderWidth + Padding.Top + Padding.Bottom;
2022-02-01 22:21:30 +01:00
if (Child != null)
{
Child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
width += Child.DesiredSize.Width;
height += Child.DesiredSize.Height;
}
2022-02-05 15:52:17 +01:00
var minWidth = BorderWidth + Math.Max(
2022-02-01 22:21:30 +01:00
CornerRadius.TopLeft + CornerRadius.TopRight,
CornerRadius.BottomLeft + CornerRadius.BottomRight + ArrowSize.Width);
2022-02-05 15:52:17 +01:00
var minHeight = BorderWidth + Math.Max(
2022-02-01 22:21:30 +01:00
CornerRadius.TopLeft + CornerRadius.BottomLeft,
CornerRadius.TopRight + CornerRadius.BottomRight);
return new Size(
2022-02-05 15:52:17 +01:00
Math.Max(width, minWidth),
Math.Max(height, minHeight) + ArrowSize.Height);
2022-02-01 22:21:30 +01:00
}
protected override Size ArrangeOverride(Size size)
{
2024-06-01 11:27:13 +02:00
Child?.Arrange(new Rect(
BorderWidth + Padding.Left,
BorderWidth + Padding.Top,
Child.DesiredSize.Width,
Child.DesiredSize.Height));
2022-02-01 22:21:30 +01:00
2024-06-01 11:27:13 +02:00
return DesiredSize;
2022-02-01 22:21:30 +01:00
}
protected override void OnRender(DrawingContext drawingContext)
{
2022-02-04 21:54:35 +01:00
var pen = new Pen
{
Brush = BorderBrush,
2022-02-05 15:52:17 +01:00
Thickness = BorderWidth,
2022-02-04 21:54:35 +01:00
LineJoin = PenLineJoin.Round
};
2022-02-05 15:52:17 +01:00
drawingContext.DrawGeometry(Background, pen, BuildGeometry());
2022-02-04 21:54:35 +01:00
base.OnRender(drawingContext);
}
2022-02-01 22:21:30 +01:00
}
}