所以我基本上有这个 ListView,我想按 Tab 并迭代我的 TreeViewItems(最好只有我的 TextBoxes)
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="number" />
<GridViewColumn Header="Selector">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding SelectorName}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
我看到的场景是第一次按下选项卡后,整个第一个 TreeViewItem 被选中,再次按下选项卡,第一个文本框被选中。最后,第三个选项卡从 TreeView 中移出到下一个控件,尽管还有更多的文本框我想在“标记”到下一个控件之前捕获它。 谢谢
编辑: 问题在这里得到了回答: 如何在 ListView 中通过 TextBox 进行 TAB 操作
也许我遗漏了一些东西,但我找不到任何简单的方法来做到这一点,这里是你可以做什么的概述:
<ListView.InputBindings>
<KeyBinding Key="Tab" Command="{Binding GoToNextItem}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ListView}}" />
<KeyBinding Modifiers="Shift" Key="Tab" Command="{Binding GoToPreviousItem}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ListView}}" />
</ListView.InputBindings>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<EventSetter Event="Selected" Handler="ItemSelected" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="number" />
<GridViewColumn Header="Selector">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Name="_tb" Text="{Binding SelectorName}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
我在这里做的事情:
代码:
private readonly ICommand _GoToNextItem = new Command((p) =>
{
var lv = p as ListView;
if (lv.SelectedIndex == -1 || lv.SelectedIndex == lv.Items.Count - 1)
{
lv.SelectedIndex = 0;
}
else
{
lv.SelectedIndex++;
}
});
public ICommand GoToNextItem { get { return _GoToNextItem; } }
private readonly ICommand _GoToPreviousItem = new Command((p) =>
{
var lv = p as ListView;
if (lv.SelectedIndex <= 0)
{
lv.SelectedIndex = lv.Items.Count - 1;
}
else
{
lv.SelectedIndex--;
}
});
public ICommand GoToPreviousItem { get { return _GoToPreviousItem; } }
private void ItemSelected(object sender, RoutedEventArgs e)
{
var item = sender as ListBoxItem;
(FindNamedChild(item, "_tb") as TextBox).Focus();
}
public static object FindNamedChild(DependencyObject container, string name)
{
if (container is FrameworkElement)
{
if ((container as FrameworkElement).Name == name) return container;
}
var ccount = VisualTreeHelper.GetChildrenCount(container);
for (int i = 0; i < ccount; i++)
{
var child = VisualTreeHelper.GetChild(container, i);
var target = FindNamedChild(child, name);
if (target != null)
{
return target;
}
}
return null;
}
这是非常粗略的,使用其中任何部分的风险由您自行承担。 (我认为,在不将选择带入其中的情况下,对焦也可以采用不同的方式完成)
(
Command
类只是ICommand
的通用实现,它采用在接口的Execute
方法中执行的lambda)
试试这个:
<ListView DataContext="List" IsTabStop="False" >
<ListView.View>
<GridView>
<GridViewColumn Header="Name" />
<GridViewColumn Header="Selector">
<GridViewColumn.CellTemplate>
<DataTemplate >
<TextBox Text="{Binding Name}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>