wpf - WPF 中的焦点数据模板

标签 wpf xaml focus datatemplate

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

问题: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>

我已经尝试过一些组合

FocusManager.FocusedElement="{Binding ElementName=[...]}"

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

最佳答案

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>

关于wpf - WPF 中的焦点数据模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/570194/

相关文章:

c# - 尝试绑定(bind) XAML 数据时出现异常

c# - WPF : emulate real input device? 中的虚拟键盘

c# - 使用正确的包 ://URI Format

c# - 不使用 WinForms 旋转光标

c# - 在 ItemsControl 中滚动,同时使用水平 StackPanel 作为 ItemsPanel

JQuery 忽略焦点上的 mouseleave

wpf - 为 RelativeSource 绑定(bind)定义混合表达式 ViewModel 定义

c# - ObservableDictionary 绑定(bind)到组合框显示值(MVVM)

objective-c - 自定义 NSButton 焦点环颜色

android - 有没有一种有效的方法来进行 "continuous"自动对焦?