From 6ed12218d42b0e6a0aca0b28cec10863040d0af8 Mon Sep 17 00:00:00 2001 From: walterlv Date: Mon, 22 Jan 2024 11:49:31 +0800 Subject: [PATCH] Fix an issue that the ItemsControl overrides control's existed automation peer --- .../PresentationFramework.csproj | 1 + .../ItemsControlElementAutomationPeer.cs | 32 +++++++++++++++++++ .../ItemsControlWrapperAutomationPeer.cs | 14 ++++++++ 3 files changed, 47 insertions(+) create mode 100644 src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlElementAutomationPeer.cs diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj index 91c17599028..df9406764c6 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj @@ -469,6 +469,7 @@ + diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlElementAutomationPeer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlElementAutomationPeer.cs new file mode 100644 index 00000000000..25d917be2a3 --- /dev/null +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlElementAutomationPeer.cs @@ -0,0 +1,32 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Windows; +using System.Windows.Controls; + +namespace System.Windows.Automation.Peers +{ + // this class is a brother of ItemsControlItemAutomationPeer, + internal class ItemsControlElementAutomationPeer : ItemAutomationPeer + { + private AutomationPeer _elementAutomationPeer; + + public ItemsControlElementAutomationPeer(UIElement element, AutomationPeer peer, ItemsControlWrapperAutomationPeer parent) + : base(element, parent) + { + _elementAutomationPeer = peer; + } + + protected override AutomationControlType GetAutomationControlTypeCore() + { + return _elementAutomationPeer.GetAutomationControlType(); + } + + protected override string GetClassNameCore() + { + return _elementAutomationPeer.GetClassName(); + } + } +} diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlWrapperAutomationPeer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlWrapperAutomationPeer.cs index a9eb8c9264f..6b4bc266abb 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlWrapperAutomationPeer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlWrapperAutomationPeer.cs @@ -21,6 +21,20 @@ public ItemsControlWrapperAutomationPeer(ItemsControl owner) override protected ItemAutomationPeer CreateItemAutomationPeer(object item) { + if (item is UIElement element) + { + // Some UIElements have their own automation peers, so we need to check for that. + var peer = element.CreateAutomationPeer(); + if (peer is not null) + { + return new ItemsControlElementAutomationPeer(element, peer, this); + } + + // Some other UIElements don't have their own automation peers, so we treat them as ItemsControlItems. + } + + // If the item is not a UIElement, or if it is a UIElement that doesn't have its own automation peer, + // we create an ItemsControlItemAutomationPeer for it. return new ItemsControlItemAutomationPeer(item, this); }