I'm working on a project to automate one of our WPF projects that utilizes the xamDataGrid control. I'm able to drill down to the "Records" datagrid inside of xamDataGrid but I am having some difficulty trying to load a virtualized record that appears inside of the grid.
I'm implementing the ItemContainerPattern and using the FindItemByProperty method with the Name/AutomationId of the element that I am searching for but when it is virtualized in the list it always returns null. If the item is not virtualized I am able to get an element back every time however.
I've temporarily switched the data grid to use RecordContainerGenerationMode="PreLoad" and everything works but this is not a change that I can make in our production code.
Digging around the various control patterns it sounds like this would normally be resolved by using ItemContainer to return the object and if that object is virtualized it is supposed to return a placeholder object. You can then use the VirtualizedItem pattern with the Realize method on the object to force it into the visual tree. Unfortunately it appears that the VirtualizedItem control pattern is not implemented for this control.
I've been trying to utilize a combination of the Scroll pattern and the ItemContainer pattern to first scroll the element into the visual tree but even after scrolling down to it's location I am unable to retrieve it via UIA.
Any recommendations?
Hello Matt,
Thank you for your post.
I have been looking into it, but it seems like I am missing something about your scenario, so if this is an issue for you, could you please send me, an isolated sample application, where this is reproduced, so I can investigate it further and support you.
Looking forward to hearing from you.
Sorry for the delay. I've attached a sample project that reproduces the problematic behavior. There are a couple unit tests in there that demonstrate the behavior.
Line 80 in UIATests.cs will need to be updated to point at the location on your workstation where you've built the sample application.
While creating the sample application I did find a possible workaround though. We are currently applying the Automation Name and Automation Id properties via Attached Behaviors. Because the xamDataGrid uses the Recycle generation mode by default it appears that when the attached behavior change handler fires for the DataRecordPresenter elements in the Records DataGrid it sets the appropriate properties for what is currently in the visual tree. When the grid is scrolled the containers that are getting recycled are not updating via the attached behavior.
I was able to get around this by implementing a WPF style that sets the properties instead but for this particular application the preferred method of applying these properties would be via the attached behavior.
Thanks for looking into this!
I have been looking into the sample application that you have provided. It seems that the behavior that you have described, derived from the event that you are using. What I can suggest is to handle the DataContextChanged event of DataRecordPreseter in the event handler of your OnAutomateXamDataGridChanged event. In the event handler of DataContextChanged you can set the AutomationIdProperty and NameProperty for each DataRecordPreseter. I modified your sample application in to show you how you can achieve the functionality that you are looking for.
Please let me know if you need any further assistance on the matter.
Thanks! That does the trick!