シーンの設定
猫のグループと犬のグループを含む (データ駆動型) GroupBox で構成される単純な Microsoft WPF アプリケーションがあります。Cat グループと Dog グループの両方に、それぞれ 2 つのグループ アイテムが含まれています。アプリケーションを実行すると、すべて正常に表示され、グループとそのコンテンツが画面に表示されます。
アプリケーションを実行すると、次のウィンドウが生成されます。
ただし、UIAutomation テストを作成すると、グループ項目の AutomationElements が見つかりません。グループだけです。AutomationElement ルートを使用してアクセスするか、以下の画像のように UISpy.exe で表示されるのは、猫と犬のグループのみです。
個々の Cat および Dog グループ項目の子コンポーネントが存在しないため、テスト コードで AutomationElements として取得できるようにする必要があります。
[TestMethod]
public void MyTest()
{
Condition controlTypeCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Group);
var foreGroundWindow = GetForegroundWindow();
var collection = foreGroundWindow.FindAll(TreeScope.Descendants, controlTypeCondition);
foreach (AutomationElement element in collection)
{
logger.Debug("Name: " + element.Current.Name);
var children = element.FindAll(TreeScope.Children, Condition.TrueCondition);
logger.Debug("Number of children: " + children.Count);
}
}
上記は現在出力します:
名前: 猫
子供の数: 0
名前: 犬
子供の数: 0
問題の再現
この問題を再現するには、Visual Studio で新しい WPF アプリケーション (WpfApplication1 という名前) を作成し、MainWindow.xaml の内容を次のものに置き換えます。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary.MergedDictionaries>
<XmlDataProvider x:Key="data">
<x:XData>
<Animals xmlns="">
<Animal name="Felix" Species="Cat" />
<Animal name="Garfield" Species="Cat" />
<Animal name="Rex" Species="Dog" />
<Animal name="Rover" Species="Dog" />
</Animals>
</x:XData>
</XmlDataProvider>
<CollectionViewSource x:Key="AnimalsBySpecies" Source="{Binding Source={StaticResource data}, XPath=Animals/Animal}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@Species" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</ResourceDictionary>
</Window.Resources>
<ItemsControl ItemsSource="{Binding Source={StaticResource AnimalsBySpecies}}">
<ItemsControl.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<GroupBox Header="{Binding Name}">
<ItemsPresenter />
</GroupBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ItemsControl.GroupStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=@name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Window>
実際には、MVVM アプリケーションであるため、私のコードはこのようには見えません。ただし、簡潔にするために、同じ問題を再現する単一の XAML ファイルに簡略化しました。注意すべき重要な点は、グループのコンテンツが XAML バインディングから設定されることです。
では、UIAutomation を使用してコンテンツにアクセスするにはどうすればよいでしょうか?
どんな助けでも大歓迎です!