将自定义ControlTemplate应用于TabControl时,屏幕阅读器不会读取WPF TabItem内容

时间:2015-10-02 11:05:09

标签: c# wpf tabcontrol controltemplate screen-readers

ControlTemplate取自Microsoft's website

<Window.Resources>
    <ControlTemplate x:Key="TabTemplate" TargetType="TabControl">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Border BorderThickness="0,0,1,1" BorderBrush="#D0CEBF" Grid.Row="1">
                <Border BorderThickness="{TemplateBinding BorderThickness}" 
                        BorderBrush="{TemplateBinding BorderBrush}">
                    <Border Background="{TemplateBinding Background}">
                        <ContentPresenter ContentSource="SelectedContent"/>
                    </Border>
                </Border>
            </Border>
            <TabPanel Grid.Row="0" IsItemsHost="true"/>
        </Grid>
    </ControlTemplate>
</Window.Resources>
<TabControl x:Key="TabTemplate">
    <TabItem Header="Header">
        <TextBlock Focusable="True" Text="Some text"/>
    </TabItem>
</TabControl>

如果我从TabControl中删除x:Key="TabTemplate",则屏幕阅读器会读取内容。

我应该向ControlTemplate添加一些内容以使其可用于屏幕阅读器吗?

1 个答案:

答案 0 :(得分:0)

<ControlTemplate x:Key="TabTemplate" TargetType="TabControl">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Border BorderThickness="0,0,1,1" BorderBrush="#D0CEBF" Grid.Row="1">
            <Border BorderThickness="{TemplateBinding BorderThickness}" 
                    BorderBrush="{TemplateBinding BorderBrush}">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent"/>
                </Border>
            </Border>
        </Border>
        <TabPanel Grid.Row="0" IsItemsHost="true"/>
    </Grid>
</ControlTemplate>

技巧是ContentPresenter的x:Name属性。屏幕阅读器的行为应正好PART_SelectedContentHost