MapItem mouse/pointer event handling

This commit is contained in:
ClemensFischer 2025-03-12 19:45:08 +01:00
parent 417d4c8987
commit fc242ee7a4
3 changed files with 27 additions and 11 deletions

View file

@ -9,10 +9,19 @@
DependencyPropertyHelper.AddOwner<MapItem, Location>(MapPanel.LocationProperty, null, DependencyPropertyHelper.AddOwner<MapItem, Location>(MapPanel.LocationProperty, null,
(item, oldValue, newValue) => item.UpdateMapTransform(newValue)); (item, oldValue, newValue) => item.UpdateMapTransform(newValue));
/// <summary>
/// Replaces ListBoxItem pointer event handling by not calling base.OnPointerPressed.
/// Setting e.Handled = true generates a PointerReleased event in the parent MapItemsControl,
/// which resembles the behavior of the ListBox base class.
/// </summary>
protected override void OnPointerPressed(PointerPressedEventArgs e) protected override void OnPointerPressed(PointerPressedEventArgs e)
{ {
if (!e.Handled)
{
e.Handled = true;
(ItemsControl.ItemsControlFromItemContainer(this) as MapItemsControl)? (ItemsControl.ItemsControlFromItemContainer(this) as MapItemsControl)?
.OnItemClicked(this, e.KeyModifiers.HasFlag(KeyModifiers.Control)); .OnItemClicked(this, e.KeyModifiers.HasFlag(KeyModifiers.Control));
} }
} }
}
} }

View file

@ -18,9 +18,13 @@ namespace MapControl
DefaultStyleKeyProperty.OverrideMetadata(typeof(MapItem), new FrameworkPropertyMetadata(typeof(MapItem))); DefaultStyleKeyProperty.OverrideMetadata(typeof(MapItem), new FrameworkPropertyMetadata(typeof(MapItem)));
} }
/// <summary>
/// Replaces ListBoxItem mouse event handling by not calling base.OnMouseLeftButtonDown.
/// Setting e.Handled = true generates a MouseLeftButtonUp event in the parent MapItemsControl,
/// which resembles the behavior of the ListBox base class.
/// </summary>
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{ {
// Prevent default handling in ListBoxItem by not calling base.OnMouseLeftButtonDown.
if (!e.Handled) if (!e.Handled)
{ {
e.Handled = true; e.Handled = true;
@ -29,9 +33,13 @@ namespace MapControl
} }
} }
/// <summary>
/// Replaces ListBoxItem mouse event handling by not calling base.OnMouseRightButtonDown.
/// Setting e.Handled = true generates a MouseRightButtonUp event in the parent MapItemsControl,
/// which resembles the behavior of the ListBox base class.
/// </summary>
protected override void OnMouseRightButtonDown(MouseButtonEventArgs e) protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
{ {
// Prevent default handling in ListBoxItem by not calling base.OnMouseRightButtonDown.
e.Handled = true; e.Handled = true;
} }
} }

View file

@ -33,9 +33,14 @@ namespace MapControl
MapPanel.InitMapElement(this); MapPanel.InitMapElement(this);
} }
/// <summary>
/// Replaces ListBoxItem pointer event handling by not calling base.OnPointerPressed.
/// Setting e.Handled = true generates a PointerReleased event in the parent MapItemsControl,
/// which differs from the behavior of the ListBox base class, where neither a PointerPressed
/// nor a PointerReleased is generated.
/// </summary>
protected override void OnPointerPressed(PointerRoutedEventArgs e) protected override void OnPointerPressed(PointerRoutedEventArgs e)
{ {
// Prevent default handling in ListBoxItem by not calling base.OnPointerPressed.
if (!e.Handled) if (!e.Handled)
{ {
e.Handled = true; e.Handled = true;
@ -44,12 +49,6 @@ namespace MapControl
} }
} }
protected override void OnPointerReleased(PointerRoutedEventArgs e)
{
// Prevent default handling in ListBoxItem by not calling base.OnPointerReleased.
e.Handled = true;
}
protected override void OnApplyTemplate() protected override void OnApplyTemplate()
{ {
base.OnApplyTemplate(); base.OnApplyTemplate();