WPF 中的焦点数据模板

Sve*_*cht 2 wpf xaml focus datatemplate

我正在寻找的行为是在 ListView 中选择一个 Item 会导致聚焦第一个可聚焦的视觉子项。

问题:ItemsControler 中的数据模板化数据没有获得初始焦点。在下面的示例中,有 4 个字符串,然后通过 Datatemplate 将其填充到 TextBox 中。

例子:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Grid>
      <ListView>
         <ListView.Resources>
            <DataTemplate DataType="{x:Type sys:String}" >
               <TextBox Width="100" Text="{Binding Mode=OneWay}"/>
            </DataTemplate>
         </ListView.Resources>
         <ListView.ItemsSource>
            <x:Array Type="{x:Type sys:String}">
               <sys:String>test</sys:String>
               <sys:String>test</sys:String>
               <sys:String>test</sys:String>
               <sys:String>test</sys:String>
            </x:Array>
         </ListView.ItemsSource>
      </ListView>
   </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

我已经尝试过一些组合

FocusManager.FocusedElement="{Binding ElementName=[...]}"
Run Code Online (Sandbox Code Playgroud)

毫无意义地说:没有成功。有人知道我如何在不遍历 C# 中的可视化树的情况下获得我想要的东西吗?应该可以做到这一点,不是吗?

Jes*_*ger 5

FocusManager 对此非常有效。

            <DataTemplate x:Key="MyDataTemplate" DataType="ListBoxItem">
            <Grid>
                <WrapPanel Orientation="Horizontal" FocusManager.FocusedElement="{Binding ElementName=tbText}">
                    <CheckBox IsChecked="{Binding Path=Completed}" Margin="5" />
                    <Button Style="{StaticResource ResourceKey=DeleteButtonTemplate}" Margin="5" Click="btnDeleteItem_Click" />
                    <TextBox Name="tbText" 
                             Text="{Binding Path=Text}" 
                             Width="200" 
                             TextWrapping="Wrap" 
                             AcceptsReturn="True" 
                             Margin="5" 
                             Focusable="True"/>
                    <DatePicker Text="{Binding Path=Date}" Margin="5"/>
                </WrapPanel>
            </Grid>
        </DataTemplate>
Run Code Online (Sandbox Code Playgroud)