diff --git a/StarterNG/Views/Depot/ConsistDragging.cs b/StarterNG/Views/Depot/ConsistDragging.cs index 7616721..8b6bbc6 100644 --- a/StarterNG/Views/Depot/ConsistDragging.cs +++ b/StarterNG/Views/Depot/ConsistDragging.cs @@ -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(includeSelf: true) is { Tag: VehicleTexture texture }) ArmVehicleDrag(e, vehicleListBox, texture); } diff --git a/StarterNG/Views/Depot/VehicleBrowser.cs b/StarterNG/Views/Depot/VehicleBrowser.cs index 6bbada2..020b14d 100644 --- a/StarterNG/Views/Depot/VehicleBrowser.cs +++ b/StarterNG/Views/Depot/VehicleBrowser.cs @@ -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(includeSelf: true) is not + { Tag: VehicleTexture texture } hit) return; BuildBrowserMenu(texture, hit).Open(hit); diff --git a/StarterNG/Views/Scenarios.axaml.cs b/StarterNG/Views/Scenarios.axaml.cs index 38b2824..9258a0e 100644 --- a/StarterNG/Views/Scenarios.axaml.cs +++ b/StarterNG/Views/Scenarios.axaml.cs @@ -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(includeSelf: true) is { Tag: int } item) sceneryList.SelectedItem = item; }