Control.IsTemplateFocusTarget 是做什么的?

What does Control.IsTemplateFocusTarget do?

我一直在阅读有关 Control.IsTemplateFocusTarget 的文章,这是一篇 XAML attached property on Windows.UI.Xaml.Controls.Control

文档稀疏:

Gets or sets a value that indicates whether this element is the part of a control template that has the focus visual.

[...]

This property is for use in a ControlTemplate only. If it's set outside of a ControlTemplate, it's ignored. If this attached property is set more than once in a ControlTemplate, an exception is thrown.

据我所知,Control.IsTemplateFocusTargetthat short documentation:

中描述的差不多

如果您在 ControlTemplate 中的元素上设置 Control.IsTemplateFocusTarget="true"(例如,如果您正在重新模板化 CheckBox),视觉焦点矩形将出现在该项目上而不是整个模板周围。


有趣的是,我相信 Control.IsTemplateFocusTarget 在某些情况下也适用于数据模板,例如 ListView。

这个简单的 ListView 在其 ItemTemplate 中使用 Control.IsTemplateFocusTarget 来显示仅围绕 TextBlock 的焦点矩形:

<ListView ItemsSource="{x:Bind Items}"
                  CanDragItems="True" CanReorderItems="True" AllowDrop="True"
                  SelectionMode="None" IsItemClickEnabled="True" ItemClick="ListView_ItemClick">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <ItemsStackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="x:String">
                    <StackPanel>
                        <TextBlock Text="{x:Bind}" Control.IsTemplateFocusTarget="True" />
                        <Button Content="Foo" IsTabStop="False" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
</ListView>

有趣的是,这种行为似乎没有嵌套:如果您的 DataTemplate 加载了一个 UserControl (<MyGreatListViewItemTemplate Item="{x:Bind}" />),Control.IsTemplateFocusTarget 将不会对该代码产生任何影响。