UIA のコントロール検索の罠
◆ x:Name は UIA の Name と対応してない
◆ UIA の AutomationId になる
◆ UIA の AutomationId になる
前にもかかったのにまた引っかかって苦戦させられました
これを取得したくて
こう書いてずっと
動かない!
とやってました
Descendants じゃだめ?
Subtree にしても動かない
uia_window がちゃんととれないのでは
x:Name ってプロパティ名あってる?
とかそういう感じで何度同じところをみたことか
UIA ってけっこう特殊なものですから 動かない理由も想定外のものとかありそうで諦めかけたころに inspect で AutomationId というのを見て そういえば Name は XAML の Name と違ったような……と言う感じで思い出しました
inspect で見ると Name は空文字です
UIA での Name は表示してるテキストのことです
ラベルやボタンだと Content に当たります
x:Name で指定したものは AutomationId に設定されます
こんな XAML なら
これで TextBox と Button が取得できます
<Textbox x:Name="textbox"/>
これを取得したくて
var uia_textbox = uia_window.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "textbox"));
new PropertyCondition(AutomationElement.NameProperty, "textbox"));
こう書いてずっと
動かない!
とやってました
Descendants じゃだめ?
Subtree にしても動かない
uia_window がちゃんととれないのでは
x:Name ってプロパティ名あってる?
とかそういう感じで何度同じところをみたことか
UIA ってけっこう特殊なものですから 動かない理由も想定外のものとかありそうで諦めかけたころに inspect で AutomationId というのを見て そういえば Name は XAML の Name と違ったような……と言う感じで思い出しました
inspect で見ると Name は空文字です
UIA での Name は表示してるテキストのことです
ラベルやボタンだと Content に当たります
x:Name で指定したものは AutomationId に設定されます
<Textbox x:Name="textbox"/>
<Button Click="Button_Click">ボタン</Button>
<Button Click="Button_Click">ボタン</Button>
こんな XAML なら
private void Button_Click(object sender, RoutedEventArgs e)
{
var uia_window = AutomationElement.FromHandle(new WindowInteropHelper(this).Handle);
var uia_textbox = uia_window.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.AutomationIdProperty, "textbox"));
var uia_button = uia_window.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "ボタン"));
}
{
var uia_window = AutomationElement.FromHandle(new WindowInteropHelper(this).Handle);
var uia_textbox = uia_window.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.AutomationIdProperty, "textbox"));
var uia_button = uia_window.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "ボタン"));
}
これで TextBox と Button が取得できます