0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UI Automationで TabControl のコンテンツが見つからない

Last updated at Posted at 2024-04-18

WPF TabControlのコンテンツをControlTemplateで作成している場合、 UI Automation で コントロール内のエレメントを検索できない

<TabControl>
    <TabControl.Resources>
        <Style TargetType="TabControl">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TabControl">
                        <StackPanel Orientation="Vertical">
                            <TabPanel IsItemsHost="True"/>
                            <ContentPresenter Content="{Binding SelectedContent, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" />
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.Resources>
    <TabItem x:Name="TabItem1" Header="TabItem1">
        <TextBlock Text="TextBlock @ TabIte1" />
    </TabItem>
</TabControl>

isnpectで確認するとTextBlockがツリーにない
NG.png

ContentPresenterに名前を設定する

TabControl のパーツ

<ContentPresenter x:Name="PART_SelectedContentHost" Content="{Binding SelectedContent, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" />

OK.png

まとめ

ControlTemplate、でカスタマイズしている場合は、ContentPresenterに名前付きパーツの名前を設定する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?