为何.NET Framework 4.7.1中ListBoxItem.OnCreateAutomationPeer返回ListBoxItemWrapperAutomationPeer?
Great question! I’ve dug into this specific behavior in .NET Framework 4.7.1, and here’s the breakdown of why this design choice was made:
Stability for Virtualized Items: By default, ListBox uses UI virtualization for large datasets—meaning ListBoxItem instances get recycled as you scroll. The
ListBoxItemWrapperAutomationPeeracts as a persistent proxy between the automation framework and the actualListBoxItemAutomationPeertied to the recycled item. This prevents automation clients (like screen readers or testing tools) from hitting invalid or disposed peer instances when items are reused, keeping the automation tree consistent.Backward Compatibility Guardrails: The wrapper class was introduced to avoid breaking existing automation workflows. Before 4.7.1, some internal WPF logic depended on a specific peer hierarchy, and swapping directly to
ListBoxItemAutomationPeercould have broken tools that interacted with the automation tree in non-standard ways. The wrapper maintains the old structure while still leveraging the functionalListBoxItemAutomationPeerunder the hood.Hidden Functionality Delegation: Even though
ListBoxItemWrapperAutomationPeerdoesn’t seem to have direct functionality, it forwards all automation-related requests (including selection mode operations) to an internalListBoxItemAutomationPeerinstance. So the selection support you need isn’t missing—it’s just being routed through the wrapper to maintain stability and compatibility.
内容的提问来源于stack exchange,提问作者jchristin

