Avalonia Pushpin

This commit is contained in:
ClemensFischer 2024-05-25 15:55:31 +02:00
parent 1788da27bd
commit 8e4110b600
10 changed files with 287 additions and 133 deletions

View file

@ -3,7 +3,6 @@
// Licensed under the Microsoft Public License (Ms-PL)
using Avalonia.Controls;
using System;
namespace MapControl
{
@ -18,8 +17,6 @@ namespace MapControl
public static readonly StyledProperty<Location> LocationProperty =
DependencyPropertyHelper.AddOwner<MapContentControl, Location>(MapPanel.LocationProperty);
protected override Type StyleKeyOverride => typeof(MapContentControl);
/// <summary>
/// Gets/sets MapPanel.AutoCollapse.
/// </summary>
@ -44,6 +41,5 @@ namespace MapControl
/// </summary>
public class Pushpin : MapContentControl
{
protected override Type StyleKeyOverride => typeof(Pushpin);
}
}

View file

@ -31,7 +31,6 @@
<Compile Remove="..\Shared\MapPath.cs" />
<Compile Remove="..\Shared\MapPolyline.cs" />
<Compile Remove="..\Shared\MapPolygon.cs" />
<Compile Remove="..\Shared\PushpinBorder.cs" />
<Compile Remove="..\Shared\ViewTransform.cs" />
</ItemGroup>

View file

@ -0,0 +1,107 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using Avalonia.Controls;
using Avalonia.Media;
using System;
namespace MapControl
{
public partial class PushpinBorder : Decorator
{
public static readonly StyledProperty<CornerRadius> CornerRadiusProperty =
DependencyPropertyHelper.Register<PushpinBorder, CornerRadius>(nameof(CornerRadius), new CornerRadius());
public static readonly StyledProperty<Size> ArrowSizeProperty =
DependencyPropertyHelper.Register<PushpinBorder, Size>(nameof(ArrowSize), new Size(10d, 20d));
public static readonly StyledProperty<double> BorderWidthProperty =
DependencyPropertyHelper.Register<PushpinBorder, double>(nameof(BorderWidth));
public static readonly StyledProperty<IBrush> BackgroundProperty =
DependencyPropertyHelper.Register<PushpinBorder, IBrush>(nameof(Background));
public static readonly StyledProperty<IBrush> BorderBrushProperty =
DependencyPropertyHelper.Register<PushpinBorder, IBrush>(nameof(BorderBrush));
static PushpinBorder()
{
AffectsMeasure<PushpinBorder>(ArrowSizeProperty, BorderWidthProperty, CornerRadiusProperty);
AffectsRender<PushpinBorder>(ArrowSizeProperty, BorderWidthProperty, CornerRadiusProperty, BackgroundProperty, BorderBrushProperty);
}
private Size RenderSize => Bounds.Size;
public CornerRadius CornerRadius
{
get => GetValue(CornerRadiusProperty);
set => SetValue(CornerRadiusProperty, value);
}
public IBrush Background
{
get => GetValue(BackgroundProperty);
set => SetValue(BackgroundProperty, value);
}
public IBrush BorderBrush
{
get => GetValue(BorderBrushProperty);
set => SetValue(BorderBrushProperty, value);
}
protected override Size MeasureOverride(Size constraint)
{
var width = 2d * BorderWidth + Padding.Left + Padding.Right;
var height = 2d * BorderWidth + Padding.Top + Padding.Bottom;
if (Child != null)
{
Child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
width += Child.DesiredSize.Width;
height += Child.DesiredSize.Height;
}
var minWidth = BorderWidth + Math.Max(
CornerRadius.TopLeft + CornerRadius.TopRight,
CornerRadius.BottomLeft + CornerRadius.BottomRight + ArrowSize.Width);
var minHeight = BorderWidth + Math.Max(
CornerRadius.TopLeft + CornerRadius.BottomLeft,
CornerRadius.TopRight + CornerRadius.BottomRight);
return new Size(
Math.Max(width, minWidth),
Math.Max(height, minHeight) + ArrowSize.Height);
}
protected override Size ArrangeOverride(Size size)
{
if (Child != null)
{
Child.Arrange(new Rect(
BorderWidth + Padding.Left,
BorderWidth + Padding.Top,
size.Width - BorderWidth - Padding.Right,
size.Height - BorderWidth - Padding.Bottom));
}
return size;
}
public override void Render(DrawingContext drawingContext)
{
var pen = new Pen
{
Brush = BorderBrush,
Thickness = BorderWidth,
LineJoin = PenLineJoin.Round
};
drawingContext.DrawGeometry(Background, pen, BuildGeometry());
base.Render(drawingContext);
}
}
}

View file

@ -0,0 +1,49 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:map="clr-namespace:MapControl">
<Style Selector="map|MapContentControl">
<Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource AncestorType=map:MapBase}}"/>
<Setter Property="BorderBrush" Value="{Binding Foreground, RelativeSource={RelativeSource AncestorType=map:MapBase}}"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Template">
<ControlTemplate>
<ContentPresenter Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</ControlTemplate>
</Setter>
</Style>
<Style Selector="map|Pushpin">
<Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource AncestorType=map:MapBase}}"/>
<Setter Property="BorderBrush" Value="{Binding Foreground, RelativeSource={RelativeSource AncestorType=map:MapBase}}"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="CornerRadius" Value="5"/>
<Setter Property="Padding" Value="7,5"/>
<Setter Property="Template">
<ControlTemplate>
<map:PushpinBorder
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderWidth="{Binding BorderThickness.Left, RelativeSource={RelativeSource TemplatedParent}}"
CornerRadius="{TemplateBinding CornerRadius}"
Padding="{TemplateBinding Padding}">
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</map:PushpinBorder>
</ControlTemplate>
</Setter>
</Style>
</Styles>