Repository navigation
Fix ItemsControl Automation - #6862
Merged
Merged
Conversation
lindexi
reviewed
Aug 3, 2022
anjaligupta-dev
approved these changes
Sep 12, 2022
Contributor
|
@walterlv - Thanks for this PR! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
GroupItemcan be detected by the Automation Software because the GroupItem provides an automation peer namedGroupItemAutomationPeerand it overrides theGetChildrenCoremethod which returns a list of inner items. Debug the code and we can find that theitemsControlAPis null in an ItemsControl GroupItem.OnCreateAutomationPeerInnermethod but doesn't overrideOnCreateAutomationPeermethod so the ItemsControl will execute theUIElement.OnCreateAutomationPeermethod.This method read a switch named
Switch.System.Windows.Controls.ItemsControlDoesNotSupportAutomation. As the name says, we should set the switch tofalseif 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 theOnCreateAutomationPeerofUIElementis called which set theAutomationNotSupportedByDefaultFieldflag totrue. So theapis null and theAutomationNotSupportedByDefaultField.GetValue(this)is true so theOnCreateAutomationPeerInternalwill 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 theOnCreateAutomationPeerofUIElementis called which returns null. So we also get a null automation peer.See that? the
ItemsControl.OnCreateAutomationPeerInternalis 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.OnCreateAutomationPeermethod and change the second argument ofSetValuetofalseso that theOnCreateAutomationPeerInnerwill be called due to the if-condition.OnCreateAutomationPeerand returnsnullor correct peer according to the switch.OnCreateAutomationPeerand keep the code almost the same as the one inUIElementexcept for thetruevalue of the second argument of theSetValuemethod.For the first one. Because the
AutomationNotSupportedByDefaultField.SetValue(this, true);statement is in theItemsControlspecific 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
ItemsControlso I limit the impact to inside theItemsControl. But it's not perfect because theItemsControl.OnCreateAutomationPeerInnerwill 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
ItemsControland 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 theItemsControland 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
ItemsControlwith 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