Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #6861
Main PR
Description
Why does this issue happen?
Let's see the code step by step.
GroupItem
can be detected by the Automation Software because the GroupItem provides an automation peer namedGroupItemAutomationPeer
and it overrides theGetChildrenCore
method which returns a list of inner items. Debug the code and we can find that theitemsControlAP
is null in an ItemsControl GroupItem.OnCreateAutomationPeerInner
method but doesn't overrideOnCreateAutomationPeer
method so the ItemsControl will execute theUIElement.OnCreateAutomationPeer
method.This method read a switch named
Switch.System.Windows.Controls.ItemsControlDoesNotSupportAutomation
. As the name says, we should set the switch tofalse
if we want the ItemsControl provides inner items automation normally. But actually, it does not work. Let me explain why it is broken.CreateAutomationPeer
. If this flag is set tofalse
, the first if-condition goes in, then theOnCreateAutomationPeer
ofUIElement
is called which set theAutomationNotSupportedByDefaultField
flag totrue
. So theap
is null and theAutomationNotSupportedByDefaultField.GetValue(this)
is true so theOnCreateAutomationPeerInternal
will not be called. As a result, we will get a null automation peer.If the flag is set to
true
(which is not expected), the else-condition goes in, then theOnCreateAutomationPeer
ofUIElement
is called which returns null. So we also get a null automation peer.See that? the
ItemsControl.OnCreateAutomationPeerInternal
is never called in this if-else and anywhere else. So this bug appears.How does this PR fix it?
We have three alternative plans to fix this issue:
UIElement.OnCreateAutomationPeer
method and change the second argument ofSetValue
tofalse
so that theOnCreateAutomationPeerInner
will be called due to the if-condition.OnCreateAutomationPeer
and returnsnull
or correct peer according to the switch.OnCreateAutomationPeer
and keep the code almost the same as the one inUIElement
except for thetrue
value of the second argument of theSetValue
method.For the first one. Because the
AutomationNotSupportedByDefaultField.SetValue(this, true);
statement is in theItemsControl
specific switch so I can guess that this statement is only written to work for ItemsControl. But it is the opposite actually. The code will execute for any other controls not only for theItemsControl
. I don't know the impact of other controls so I give up the plan.For the second one, the code only works for the
ItemsControl
so I limit the impact to inside theItemsControl
. But it's not perfect because theItemsControl.OnCreateAutomationPeerInner
will never be called and should be deleted. However, it has a special bug description comment on the method which is hard to understand.For the third one, the impact is limited to
ItemsControl
and the execution steps are exactly the same as the version before this PR. But I don't like this code because I think it's a bug of theItemsControl
and this code keeps the bug hidden inside.In conclusion, I choose the second plan to fix it.
Customer Impact
If this issue is not fixed, the automation software cannot detect the items under groups of ItemsControls.
See the image, please. The first one is the original ItemsControl in the current dotnet version and the second one is the issue-fixed version. We can see the automation software cannot detect the inner item in the ItemsControl on the current dotnet version while everything works fine on the issue-fixed version.
Regression
No. It's a long-time bug.
Testing
The new code of this pull request has been tested in the repo manually: https://github.com/walterlv/Walterlv.WpfIssues.ItemsControlAutomationIssue
I write a custom
ItemsControl
with only automation methods overridden. I've tested the demo app and find that the bug is fixed (as the picture above says).Risk
Maybe I've written all the risks in the description section. Maybe I need some conversations to make the plans more beautiful and make less risk.
Microsoft Reviewers: Open in CodeFlow