From 0d120fc115a1f22604d9ed6d298c516f38f27f13 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 29 May 2020 14:38:50 +0200 Subject: [PATCH] Hack selecting controls when switching tree. --- .../Diagnostics/ViewModels/MainViewModel.cs | 17 ++++++++++++++++- .../Diagnostics/ViewModels/TreePageViewModel.cs | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs index 7addaba977..1d19e1a346 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs @@ -2,6 +2,7 @@ using Avalonia.Controls; using Avalonia.Diagnostics.Models; using Avalonia.Input; +using Avalonia.Threading; namespace Avalonia.Diagnostics.ViewModels { @@ -49,7 +50,21 @@ namespace Avalonia.Diagnostics.ViewModels value is TreePageViewModel newTree && oldTree?.SelectedNode?.Visual is IControl control) { - newTree.SelectControl(control); + // HACK: We want to select the currently selected control in the new tree, but + // to select nested nodes in TreeView, currently the TreeView has to be able to + // expand the parent nodes. Because at this point the TreeView isn't visible, + // this will fail unless we schedule the selection to run after layout. + DispatcherTimer.RunOnce( + () => + { + try + { + newTree.SelectControl(control); + } + catch { } + }, + TimeSpan.FromMilliseconds(0), + DispatcherPriority.ApplicationIdle); } RaiseAndSetIfChanged(ref _content, value); diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreePageViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreePageViewModel.cs index 4496cc0a1c..38ac88a83c 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreePageViewModel.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreePageViewModel.cs @@ -95,8 +95,8 @@ namespace Avalonia.Diagnostics.ViewModels if (node != null) { - Selection.SelectedIndex = node.Index; ExpandNode(node.Parent); + Selection.SelectedIndex = node.Index; } }