Skip to content

Commit daf7e8d

Browse files
committed
Improve UIA Provider Cleanup Logic for Virtual ListView Items
1 parent e36dddd commit daf7e8d

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListView.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ public partial class ListView : Control
208208
// We cache the NewWidth supplied by the user and use it on HDN_ENDTRACK to set the final column width.
209209
private int _newWidthForColumnWidthChangingCancelled = -1;
210210

211+
// Tracks ListViewItems that have had their AccessibilityObject created in virtual mode,
212+
// so their UIA providers can be properly released later without triggering item retrieval
213+
private readonly HashSet<ListViewItem> _uiaCreatedItems = [];
214+
211215
/// <summary>
212216
/// Creates an empty ListView with default styles.
213217
/// </summary>
@@ -5084,16 +5088,43 @@ public void RedrawItems(int startIndex, int endIndex, bool invalidateOnly)
50845088
}
50855089
}
50865090

5091+
internal void NotifyUiaCreated(ListViewItem item)
5092+
{
5093+
if (VirtualMode)
5094+
{
5095+
_uiaCreatedItems.Add(item);
5096+
}
5097+
}
5098+
50875099
internal override void ReleaseUiaProvider(HWND handle)
50885100
{
50895101
if (!OsVersion.IsWindows8OrGreater())
50905102
{
50915103
return;
50925104
}
50935105

5094-
for (int i = 0; i < Items.Count; i++)
5106+
if (VirtualMode)
50955107
{
5096-
Items.GetItemByIndex(i)?.ReleaseUiaProvider();
5108+
foreach (ListViewItem item in _uiaCreatedItems)
5109+
{
5110+
if (item.IsAccessibilityObjectCreated)
5111+
{
5112+
item.ReleaseUiaProvider();
5113+
}
5114+
}
5115+
5116+
_uiaCreatedItems.Clear();
5117+
}
5118+
else
5119+
{
5120+
for (int i = 0; i < Items.Count; i++)
5121+
{
5122+
var item = Items.GetItemByIndex(i);
5123+
if (item?.IsAccessibilityObjectCreated == true)
5124+
{
5125+
item.ReleaseUiaProvider();
5126+
}
5127+
}
50975128
}
50985129

50995130
if (_defaultGroup is not null)

src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListViewItem.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,12 @@ internal virtual AccessibleObject AccessibilityObject
265265
};
266266
}
267267

268+
owningListView.NotifyUiaCreated(this);
268269
return _accessibilityObject;
269270
}
270271
}
271272

272-
private bool IsAccessibilityObjectCreated => _accessibilityObject is not null;
273+
internal bool IsAccessibilityObjectCreated => _accessibilityObject is not null;
273274

274275
/// <summary>
275276
/// The font that this item will be displayed in. If its value is null, it will be displayed
@@ -1011,7 +1012,7 @@ internal void Host(ListView parent, int id, int index)
10111012

10121013
internal void ReleaseUiaProvider()
10131014
{
1014-
if (!IsAccessibilityObjectCreated)
1015+
if (IsAccessibilityObjectCreated)
10151016
{
10161017
return;
10171018
}

0 commit comments

Comments
 (0)