From a29bfdac72c0433e40e50a54c9a04d2c91d21651 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 21 Jan 2023 14:29:48 +0100 Subject: [PATCH] Clarify this code. --- .../Styling/Activators/NthChildActivator.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Avalonia.Base/Styling/Activators/NthChildActivator.cs b/src/Avalonia.Base/Styling/Activators/NthChildActivator.cs index 6c18081287..e0fa056765 100644 --- a/src/Avalonia.Base/Styling/Activators/NthChildActivator.cs +++ b/src/Avalonia.Base/Styling/Activators/NthChildActivator.cs @@ -52,8 +52,21 @@ namespace Avalonia.Styling.Activators // 2. Subscribed child index was changed. if (e.Child is null || e.Child == _control) { + // We're using the _index field to pass the index of the child to EvaluateIsActive + // *only* when the active state is re-evaluated via this event handler. The docs + // for EvaluateIsActive say: + // + // > This method should read directly from its inputs and not rely on any + // > subscriptions to fire in order to be up-to-date. + // + // Which is good advice in general, however in this case we need to break the rule + // and use the value from the event subscription instead of calling + // IChildIndexProvider.GetChildIndex. This is because this event can be fired during + // the process of realizing an element of a virtualized list; in this case calling + // GetChildIndex may not return the correct index as the element isn't yet realized. _index = e.Index; ReevaluateIsActive(); + _index = -1; } }