XAML-Map-Control/MapControl/WinUI/MapItem.WinUI.cs

99 lines
2.9 KiB
C#
Raw Normal View History

2025-03-19 11:14:13 +01:00
using System;
using Windows.System;
2024-05-22 11:25:32 +02:00
#if UWP
using Windows.UI.Xaml;
2025-03-19 11:14:13 +01:00
using Windows.UI.Xaml.Controls;
2024-05-26 21:25:36 +02:00
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
2024-05-22 11:25:32 +02:00
#else
using Microsoft.UI.Xaml;
2025-03-19 11:14:13 +01:00
using Microsoft.UI.Xaml.Controls;
2024-05-26 21:25:36 +02:00
using Microsoft.UI.Xaml.Data;
2024-05-22 11:25:32 +02:00
using Microsoft.UI.Xaml.Input;
#endif
2026-04-13 17:14:49 +02:00
namespace MapControl;
public partial class MapItem
{
2026-04-13 17:14:49 +02:00
private Windows.Foundation.Point? pointerPressedPosition;
public MapItem()
{
2026-04-13 17:14:49 +02:00
DefaultStyleKey = typeof(MapItem);
MapPanel.InitMapElement(this);
}
2025-03-19 11:14:13 +01:00
2026-04-13 17:14:49 +02:00
protected override void OnPointerPressed(PointerRoutedEventArgs e)
{
base.OnPointerPressed(e);
var point = e.GetCurrentPoint(null);
if (point.Properties.IsLeftButtonPressed)
{
pointerPressedPosition = point.Position;
}
2026-04-13 17:14:49 +02:00
e.Handled = true;
}
2026-04-13 17:14:49 +02:00
protected override void OnPointerReleased(PointerRoutedEventArgs e)
{
if (pointerPressedPosition.HasValue)
2025-03-19 11:14:13 +01:00
{
const double pointerMovementThreshold = 2d;
var position = e.GetCurrentPoint(null).Position;
2025-03-19 11:14:13 +01:00
2026-04-13 17:14:49 +02:00
// Perform selection only when no significant pointer movement occured.
//
if (Math.Abs(position.X - pointerPressedPosition.Value.X) <= pointerMovementThreshold &&
Math.Abs(position.Y - pointerPressedPosition.Value.Y) <= pointerMovementThreshold)
{
if (ItemsControl.ItemsControlFromItemContainer(this) is MapItemsControl mapItemsControl &&
mapItemsControl.SelectionMode == SelectionMode.Extended &&
2026-04-13 17:14:49 +02:00
e.KeyModifiers.HasFlag(VirtualKeyModifiers.Shift))
2025-03-19 11:14:13 +01:00
{
2026-04-13 17:14:49 +02:00
mapItemsControl.SelectItemsInRange(this);
}
else
{
base.OnPointerReleased(e);
2025-03-19 11:14:13 +01:00
}
2025-03-12 19:23:05 +01:00
}
2025-03-19 11:14:13 +01:00
2026-04-13 17:14:49 +02:00
pointerPressedPosition = null;
2025-03-12 19:23:05 +01:00
}
2026-04-13 17:14:49 +02:00
e.Handled = true;
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
2026-04-13 17:14:49 +02:00
var parentMap = MapPanel.GetParentMap(this);
2026-04-13 17:14:49 +02:00
if (parentMap != null)
{
// Workaround for missing RelativeSource AncestorType=MapBase Bindings in default Style.
//
if (Background == null)
{
2026-04-13 17:14:49 +02:00
SetBinding(BackgroundProperty,
new Binding { Source = parentMap, Path = new PropertyPath(nameof(Background)) });
}
if (Foreground == null)
{
SetBinding(ForegroundProperty,
new Binding { Source = parentMap, Path = new PropertyPath(nameof(Foreground)) });
}
if (BorderBrush == null)
{
SetBinding(BorderBrushProperty,
new Binding { Source = parentMap, Path = new PropertyPath(nameof(Foreground)) });
}
}
}
}