XAML-Map-Control/MapControl/Avalonia/MapItemsControl.Avalonia.cs

76 lines
2.2 KiB
C#
Raw Normal View History

2026-04-10 16:55:33 +02:00
using Avalonia;
using Avalonia.Controls;
2025-08-19 19:43:02 +02:00
using Avalonia.Controls.Presenters;
2024-05-27 16:35:02 +02:00
using Avalonia.Controls.Templates;
2025-08-19 19:43:02 +02:00
using Avalonia.Input;
2026-04-10 16:55:33 +02:00
using Avalonia.Interactivity;
2025-08-19 19:43:02 +02:00
using Avalonia.Media;
2024-05-27 16:35:02 +02:00
2026-04-13 17:14:49 +02:00
namespace MapControl;
public partial class MapItemsControl
2024-05-27 16:35:02 +02:00
{
2026-04-13 17:14:49 +02:00
static MapItemsControl()
2024-05-27 16:35:02 +02:00
{
2026-04-13 17:14:49 +02:00
TemplateProperty.OverrideDefaultValue<MapItemsControl>(
new FuncControlTemplate<MapItemsControl>(
(itemsControl, namescope) => new ItemsPresenter { ItemsPanel = itemsControl.ItemsPanel }));
2024-05-27 16:35:02 +02:00
2026-04-13 17:14:49 +02:00
ItemsPanelProperty.OverrideDefaultValue<MapItemsControl>(
new FuncTemplate<Panel>(() => new MapPanel()));
}
2024-05-27 16:35:02 +02:00
2026-04-13 17:14:49 +02:00
public void SelectItemsInGeometry(Geometry geometry)
{
SelectItemsByPosition(geometry.FillContains);
}
2024-05-27 16:35:02 +02:00
2026-04-13 17:14:49 +02:00
public new MapItem ContainerFromItem(object item)
{
return (MapItem)base.ContainerFromItem(item);
}
2025-06-05 19:06:38 +02:00
2026-04-13 17:14:49 +02:00
protected override bool NeedsContainerOverride(object item, int index, out object recycleKey)
{
recycleKey = null;
2024-05-27 16:35:02 +02:00
2026-04-13 17:14:49 +02:00
return item is not MapItem;
}
2024-05-27 16:35:02 +02:00
2026-04-13 17:14:49 +02:00
protected override Control CreateContainerForItemOverride(object item, int index, object recycleKey)
{
return new MapItem();
}
2024-05-27 16:35:02 +02:00
2026-04-13 17:14:49 +02:00
protected override void PrepareContainerForItemOverride(Control container, object item, int index)
{
base.PrepareContainerForItemOverride(container, item, index);
PrepareContainer((MapItem)container, item);
}
2024-05-27 16:35:02 +02:00
2026-04-13 17:14:49 +02:00
protected override void ClearContainerForItemOverride(Control container)
{
base.ClearContainerForItemOverride(container);
ClearContainer((MapItem)container);
}
protected override bool ShouldTriggerSelection(Visual selectable, PointerEventArgs e)
2026-04-13 17:14:49 +02:00
{
return e.Properties.PointerUpdateKind == PointerUpdateKind.LeftButtonReleased;
2026-04-13 17:14:49 +02:00
}
2025-03-19 11:14:13 +01:00
public override bool UpdateSelectionFromEvent(UIElement container, RoutedEventArgs e)
2026-04-13 17:14:49 +02:00
{
if (SelectionMode == SelectionMode.Multiple &&
e is PointerEventArgs p &&
p.KeyModifiers.HasFlag(KeyModifiers.Shift) &&
ShouldTriggerSelection(container, p))
2025-03-19 11:14:13 +01:00
{
2026-04-13 17:14:49 +02:00
SelectItemsInRange((MapItem)container);
2026-04-10 16:55:33 +02:00
return true;
}
return base.UpdateSelectionFromEvent(container, e);
2024-05-27 16:35:02 +02:00
}
}