Avalonia MapItemsControl

This commit is contained in:
ClemensFischer 2024-05-27 16:35:02 +02:00
parent 39e00b30d9
commit e9e0393074
11 changed files with 206 additions and 68 deletions

View file

@ -16,13 +16,13 @@ global using Avalonia.Media.Imaging;
global using Avalonia.Platform;
global using Avalonia.Styling;
global using Avalonia.Threading;
global using Brush = Avalonia.Media.IBrush;
global using ImageSource = Avalonia.Media.IImage;
global using DependencyObject = Avalonia.AvaloniaObject;
global using DependencyProperty = Avalonia.AvaloniaProperty;
global using FrameworkElement = Avalonia.Controls.Control;
global using HorizontalAlignment = Avalonia.Layout.HorizontalAlignment;
global using VerticalAlignment = Avalonia.Layout.VerticalAlignment;
global using Brush = Avalonia.Media.IBrush;
global using ImageSource = Avalonia.Media.IImage;
global using PathFigureCollection = Avalonia.Media.PathFigures;
global using PointCollection = System.Collections.Generic.List<Avalonia.Point>;
global using PropertyPath = System.String;

View file

@ -24,8 +24,6 @@
<ItemGroup>
<Compile Include="..\Shared\*.cs" />
<Compile Remove="..\Shared\GeoImage.cs" />
<Compile Remove="..\Shared\MapItem.cs" />
<Compile Remove="..\Shared\MapItemsControl.cs" />
<Compile Remove="..\Shared\ViewTransform.cs" />
</ItemGroup>

View file

@ -0,0 +1,24 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
namespace MapControl
{
public partial class MapItem
{
public static readonly StyledProperty<bool> AutoCollapseProperty =
DependencyPropertyHelper.AddOwner<MapItem, bool>(MapPanel.AutoCollapseProperty);
public static readonly StyledProperty<Location> LocationProperty =
DependencyPropertyHelper.AddOwner<MapItem, Location>(MapPanel.LocationProperty, null,
(item, oldValue, newValue) => item.UpdateMapTransform(newValue));
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
(ItemsControl.ItemsControlFromItemContainer(this) as MapItemsControl)?.OnItemClicked(
this, e.KeyModifiers.HasFlag(KeyModifiers.Control), e.KeyModifiers.HasFlag(KeyModifiers.Shift));
}
}
}

View file

@ -0,0 +1,64 @@
// 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.Presenters;
using Avalonia.Controls.Templates;
namespace MapControl
{
public partial class MapItemsControl
{
static MapItemsControl()
{
TemplateProperty.OverrideDefaultValue<MapItemsControl>(
new FuncControlTemplate<MapItemsControl>(
(itemsControl, namescope) => new ItemsPresenter { ItemsPanel = itemsControl.ItemsPanel }));
ItemsPanelProperty.OverrideDefaultValue<MapItemsControl>(
new FuncTemplate<Panel>(() => new MapPanel()));
}
public void SelectItemsInGeometry(Geometry geometry)
{
SelectItemsByPosition(p => geometry.FillContains(p));
}
protected override bool NeedsContainerOverride(object item, int index, out object recycleKey)
{
recycleKey = null;
return item is not MapItem;
}
protected override Control CreateContainerForItemOverride(object item, int index, object recycleKey)
{
return new MapItem();
}
protected override void PrepareContainerForItemOverride(Control container, object item, int index)
{
base.PrepareContainerForItemOverride(container, item, index);
if (LocationMemberPath != null && container is MapItem mapItem)
{
mapItem.SetBinding(MapItem.LocationProperty,
new Binding
{
Path = new PropertyPath(LocationMemberPath),
Source = item
});
}
}
protected override void ClearContainerForItemOverride(Control container)
{
base.ClearContainerForItemOverride(container);
if (LocationMemberPath != null && container is MapItem mapItem)
{
mapItem.ClearValue(MapItem.LocationProperty);
}
}
}
}

View file

@ -2,6 +2,24 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:map="clr-namespace:MapControl">
<!-- Style replaced by TemplateProperty and ItemsPanelProperty default value overrides -->
<!--
<Style Selector="map|MapItemsControl">
<Setter Property="Template">
<ControlTemplate>
<ItemsPresenter ItemsPanel="{TemplateBinding ItemsPanel}"/>
</ControlTemplate>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<map:MapPanel/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
-->
<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}}"/>

View file

@ -6,15 +6,12 @@ using System;
#if WPF
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
#elif UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
#endif
namespace MapControl
@ -36,41 +33,6 @@ namespace MapControl
set => SetValue(LocationMemberPathProperty, value);
}
protected override DependencyObject GetContainerForItemOverride()
{
return new MapItem();
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is MapItem;
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
if (LocationMemberPath != null && element is MapItem mapItem)
{
mapItem.SetBinding(MapItem.LocationProperty,
new Binding
{
Path = new PropertyPath(LocationMemberPath),
Source = item
});
}
}
protected override void ClearContainerForItemOverride(DependencyObject element, object item)
{
base.ClearContainerForItemOverride(element, item);
if (LocationMemberPath != null && element is MapItem mapItem)
{
mapItem.ClearValue(MapItem.LocationProperty);
}
}
public void SelectItems(Predicate<object> predicate)
{
if (SelectionMode == SelectionMode.Single)
@ -100,8 +62,9 @@ namespace MapControl
{
SelectItems(item =>
{
var loc = MapPanel.GetLocation(ContainerFromItem(item));
return loc != null && predicate(loc);
var location = MapPanel.GetLocation(ContainerFromItem(item));
return location != null && predicate(location);
});
}
@ -119,7 +82,7 @@ namespace MapControl
SelectItemsByPosition(p => rect.Contains(p));
}
protected internal void OnItemClicked(FrameworkElement mapItem, bool controlKey, bool shiftKey)
protected internal void OnItemClicked(MapItem mapItem, bool controlKey, bool shiftKey)
{
var item = ItemFromContainer(mapItem);

View file

@ -25,7 +25,7 @@ namespace MapControl
/// <summary>
/// Gets or sets the Locations that define the polygon points.
/// </summary>
#if WPF || AVALONIA
#if WPF
[System.ComponentModel.TypeConverter(typeof(LocationCollectionConverter))]
#endif
public IEnumerable<Location> Locations

View file

@ -25,7 +25,7 @@ namespace MapControl
/// <summary>
/// Gets or sets the Locations that define the polyline points.
/// </summary>
#if WPF || AVALONIA
#if WPF
[System.ComponentModel.TypeConverter(typeof(LocationCollectionConverter))]
#endif
public IEnumerable<Location> Locations

View file

@ -4,6 +4,7 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace MapControl
@ -15,12 +16,12 @@ namespace MapControl
DefaultStyleKeyProperty.OverrideMetadata(typeof(MapItemsControl), new FrameworkPropertyMetadata(typeof(MapItemsControl)));
}
public FrameworkElement ContainerFromItem(object item)
public MapItem ContainerFromItem(object item)
{
return (FrameworkElement)ItemContainerGenerator.ContainerFromItem(item);
return (MapItem)ItemContainerGenerator.ContainerFromItem(item);
}
public object ItemFromContainer(FrameworkElement container)
public object ItemFromContainer(MapItem container)
{
return ItemContainerGenerator.ItemFromContainer(container);
}
@ -29,5 +30,40 @@ namespace MapControl
{
SelectItemsByPosition(p => geometry.FillContains(p));
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is MapItem;
}
protected override DependencyObject GetContainerForItemOverride()
{
return new MapItem();
}
protected override void PrepareContainerForItemOverride(DependencyObject container, object item)
{
base.PrepareContainerForItemOverride(container, item);
if (LocationMemberPath != null && container is MapItem mapItem)
{
mapItem.SetBinding(MapItem.LocationProperty,
new Binding
{
Path = new PropertyPath(LocationMemberPath),
Source = item
});
}
}
protected override void ClearContainerForItemOverride(DependencyObject container, object item)
{
base.ClearContainerForItemOverride(container, item);
if (LocationMemberPath != null && container is MapItem mapItem)
{
mapItem.ClearValue(MapItem.LocationProperty);
}
}
}
}

View file

@ -4,8 +4,10 @@
#if UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
#else
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
#endif
namespace MapControl
@ -18,9 +20,44 @@ namespace MapControl
MapPanel.InitMapElement(this);
}
public new FrameworkElement ContainerFromItem(object item)
public new MapItem ContainerFromItem(object item)
{
return (FrameworkElement)base.ContainerFromItem(item);
return (MapItem)base.ContainerFromItem(item);
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is MapItem;
}
protected override DependencyObject GetContainerForItemOverride()
{
return new MapItem();
}
protected override void PrepareContainerForItemOverride(DependencyObject container, object item)
{
base.PrepareContainerForItemOverride(container, item);
if (LocationMemberPath != null && container is MapItem mapItem)
{
mapItem.SetBinding(MapItem.LocationProperty,
new Binding
{
Path = new PropertyPath(LocationMemberPath),
Source = item
});
}
}
protected override void ClearContainerForItemOverride(DependencyObject container, object item)
{
base.ClearContainerForItemOverride(container, item);
if (LocationMemberPath != null && container is MapItem mapItem)
{
mapItem.ClearValue(MapItem.LocationProperty);
}
}
}
}

View file

@ -12,25 +12,23 @@
Center="53.5,8.2"
DoubleTapped="OnMapDoubleTapped">
<ItemsControl ItemsSource="{Binding Pushpins}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<map:MapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Styles>
<Style Selector="ContentPresenter">
<map:MapItemsControl ItemsSource="{Binding Pushpins}" Background="{x:Null}">
<map:MapItemsControl.Styles>
<Style Selector="map|MapItem">
<Setter Property="map:MapPanel.Location" Value="{Binding Location}"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="map:MapPanel.Location" Value="{Binding Location}"/>
<Setter Property="Template">
<ControlTemplate>
<map:Pushpin Content="{Binding Name}"/>
</ControlTemplate>
</Setter>
</Style>
</ItemsControl.Styles>
<ItemsControl.ItemTemplate>
<DataTemplate>
<map:Pushpin Content="{Binding Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Style Selector="map|MapItem[IsSelected=True]">
<Setter Property="Foreground" Value="Red"/>
</Style>
</map:MapItemsControl.Styles>
</map:MapItemsControl>
<map:MapPath Location="53.5,8.2" Stroke="Blue" StrokeThickness="3" Fill="#1F007F00">
<map:MapPath.Data>