diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListView.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListView.cs index f290e8ce8eb..c1d2a1f03b9 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListView.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListView.cs @@ -208,6 +208,9 @@ public partial class ListView : Control // We cache the NewWidth supplied by the user and use it on HDN_ENDTRACK to set the final column width. private int _newWidthForColumnWidthChangingCancelled = -1; + // Tracks indices of ListViewItems that have been accessed by UIA in virtual mode, + // so their UIA providers can be properly released later without triggering item retrieval. + private readonly HashSet _uiaAccessedIndices = []; /// /// Creates an empty ListView with default styles. /// @@ -5084,6 +5087,14 @@ public void RedrawItems(int startIndex, int endIndex, bool invalidateOnly) } } + internal void NotifyUiaCreated(int index) + { + if (VirtualMode) + { + _uiaAccessedIndices.Add(index); + } + } + internal override void ReleaseUiaProvider(HWND handle) { if (!OsVersion.IsWindows8OrGreater()) @@ -5091,9 +5102,21 @@ internal override void ReleaseUiaProvider(HWND handle) return; } - for (int i = 0; i < Items.Count; i++) + if (VirtualMode) + { + foreach (int index in _uiaAccessedIndices) + { + Items.GetItemByIndex(index)?.ReleaseUiaProvider(); + } + + _uiaAccessedIndices.Clear(); + } + else { - Items.GetItemByIndex(i)?.ReleaseUiaProvider(); + for (int i = 0; i < Items.Count; i++) + { + Items.GetItemByIndex(i)?.ReleaseUiaProvider(); + } } if (_defaultGroup is not null) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListViewItem.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListViewItem.cs index 3c8bf59e6ee..a6e44dd7233 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListViewItem.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListViewItem.cs @@ -265,6 +265,8 @@ internal virtual AccessibleObject AccessibilityObject }; } + owningListView.NotifyUiaCreated(Index); + return _accessibilityObject; } }