I have a window that contains a listbox, My setup allows a unit test to access the listbox directly.
<Window x:Class="Demo.DemoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Demo">
<ListBox
x:Name="MyListBox"
x:FieldModifier="public"
ItemsSource="{Binding ViewModelItems}"
DisplayMemberPath="Name"
/>
</Window>
I want to set a value on the view using UI Automation for my unit tests. Here is the start of a helper method that I think would be relevant for that.
public static void Select(System.Windows.Controls.ListBox listbox, string item)
{
ListBoxAutomationPeer peer = new ListBoxAutomationPeer(listbox);
IItemContainerProvider provider = peer.GetPattern(PatternInterface.ItemContainer) as IItemContainerProvider;
IRawElementProviderSimple rawItem = provider.FindItemByProperty(null, 0, item);
//rawitem is null here -> could be because I have no concept of how to use FindItemByProperty
//The rest seems incorrect - why would my highlevel UI interaction break down to IRawElementProviderSimple
//Is this Item already realized or am I working on a virtualized item that I need to realize?
ISelectionItemProvider selectable = rawItem.GetPatternProvider((int)PatternInterface.SelectionItem) as ISelectionItemProvider;
selectable.Select();
}
I seem to miss something in the documentation. There is an msdn article that explains the concepts of working with virtualized items. Since I am using the WPF wrapper classes this should be equivalent to using the IItemContainerProvider
.
My implementation does not work and I can not find the piece of documentation that would clear this up for me.
How do I select an Item In a list of items using the displayed text.