Find the list item under the pointer with FindAncestorOfType

This commit is contained in:
maj00r
2026-10-04 14:38:05 +02:00
parent 3ac792c0fa
commit a54576b975
3 changed files with 7 additions and 14 deletions

View File

@@ -5,6 +5,7 @@ using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.VisualTree;
using StarterNG.Classes;
using StarterNG.Domain;
@@ -113,10 +114,7 @@ public sealed class ConsistDragging
if (!e.GetCurrentPoint(vehicleListBox).Properties.IsLeftButtonPressed)
return;
var hit = e.Source as Control;
while (hit != null && hit is not ListBoxItem)
hit = hit.Parent as Control;
if (hit is ListBoxItem { Tag: VehicleTexture texture })
if ((e.Source as Visual).FindAncestorOfType<ListBoxItem>(includeSelf: true) is { Tag: VehicleTexture texture })
ArmVehicleDrag(e, vehicleListBox, texture);
}

View File

@@ -11,6 +11,7 @@ using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using Avalonia.VisualTree;
using StarterNG.Classes;
using StarterNG.Domain;
@@ -363,11 +364,8 @@ public sealed class VehicleBrowser
private void List_OnContextRequested(object? sender, ContextRequestedEventArgs e)
{
var hit = e.Source as Control;
while (hit != null && hit is not ListBoxItem)
hit = hit.Parent as Control;
if (hit is not ListBoxItem { Tag: VehicleTexture texture })
if ((e.Source as Visual).FindAncestorOfType<ListBoxItem>(includeSelf: true) is not
{ Tag: VehicleTexture texture } hit)
return;
BuildBrowserMenu(texture, hit).Open(hit);

View File

@@ -16,6 +16,7 @@ using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using Avalonia.VisualTree;
using StarterNG.Classes;
using StarterNG.Domain;
using StarterNG.Application;
@@ -103,11 +104,7 @@ public partial class Scenarios : UserControl
// A right click picks the scenery under it, so the menu speaks of that one.
private void SceneryList_OnContextRequested(object? sender, ContextRequestedEventArgs e)
{
var hit = e.Source as Control;
while (hit is not null and not TreeViewItem)
hit = hit.Parent as Control;
if (hit is TreeViewItem { Tag: int } item)
if ((e.Source as Visual).FindAncestorOfType<TreeViewItem>(includeSelf: true) is { Tag: int } item)
sceneryList.SelectedItem = item;
}