From f91a788d18a40c187cd09e3516d86619869429c4 Mon Sep 17 00:00:00 2001 From: dif-sam <41672086+dif-sam@users.noreply.github.com> Date: Thu, 13 Oct 2022 16:21:54 +0400 Subject: [PATCH 1/2] Suggested fix for #9173 trying to deffere request for BringIntoView, when TreeViewItem not ready yet. --- src/Avalonia.Controls/TreeViewItem.cs | 29 +++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/Avalonia.Controls/TreeViewItem.cs b/src/Avalonia.Controls/TreeViewItem.cs index ada081b808..c2304abbc8 100644 --- a/src/Avalonia.Controls/TreeViewItem.cs +++ b/src/Avalonia.Controls/TreeViewItem.cs @@ -45,6 +45,8 @@ namespace Avalonia.Controls private IControl? _header; private bool _isExpanded; private int _level; + private bool _templateApplied; + private bool _deferredBringIntoViewFlag; /// /// Initializes static members of the class. @@ -136,15 +138,24 @@ namespace Avalonia.Controls protected virtual void OnRequestBringIntoView(RequestBringIntoViewEventArgs e) { - if (e.TargetObject == this && _header != null) + if (e.TargetObject == this) { - var m = _header.TransformToVisual(this); + if (!_templateApplied) + { + _deferredBringIntoViewFlag = true; + return; + } - if (m.HasValue) + if (_header != null) { - var bounds = new Rect(_header.Bounds.Size); - var rect = bounds.TransformToAABB(m.Value); - e.TargetRect = rect; + var m = _header.TransformToVisual(this); + + if (m.HasValue) + { + var bounds = new Rect(_header.Bounds.Size); + var rect = bounds.TransformToAABB(m.Value); + e.TargetRect = rect; + } } } } @@ -187,6 +198,12 @@ namespace Avalonia.Controls protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { _header = e.NameScope.Find("PART_Header"); + _templateApplied = true; + if (_deferredBringIntoViewFlag) + { + _deferredBringIntoViewFlag = false; + Dispatcher.UIThread.Post(this.BringIntoView); // must use the Dispatcher, otherwise the TreeView doesn't scroll + } } private static int CalculateDistanceFromLogicalParent(ILogical? logical, int @default = -1) where T : class From 65d39603fac9f963324158ab8d7c420565c5785b Mon Sep 17 00:00:00 2001 From: dif-sam <41672086+dif-sam@users.noreply.github.com> Date: Thu, 13 Oct 2022 16:55:55 +0400 Subject: [PATCH 2/2] Forgotten namespase added to usings --- src/Avalonia.Controls/TreeViewItem.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Avalonia.Controls/TreeViewItem.cs b/src/Avalonia.Controls/TreeViewItem.cs index c2304abbc8..2f96e6911f 100644 --- a/src/Avalonia.Controls/TreeViewItem.cs +++ b/src/Avalonia.Controls/TreeViewItem.cs @@ -6,6 +6,7 @@ using Avalonia.Controls.Primitives; using Avalonia.Controls.Templates; using Avalonia.Input; using Avalonia.LogicalTree; +using Avalonia.Threading; namespace Avalonia.Controls {