Maybe I missed something, but I can not find any simple way to do this, here is a diagram of what you could do:
<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>
Things I did here:
- Override tab behavior for fire brigade teams to select a different item
- Add an event handler for the selected event to focus the TextBox
- The name of the TextBox so that it can be found and focused
the code:
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; }
This is very schematic; use any part of it at your own risk. (Focusing could also be done differently without introducing choice)
(The Command class is just a general implementation of ICommand , which accepts lambda, which runs in the Execute interface method)
source share