How to TAB through ListView with TextBoxes as TreeViewItems?

So, I basically have this ListView, and I would like to press Tab and iterate through my TreeViewItems (preferably only my 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> 

The scenario that I see is after clicking on the tab for the first time, when the entire first TreeViewItem is selected and pressing Tab again displays the first TextBox. Finally, the third TAB exits the TreeView into the next control, although there are more TextBox that I would like to catch before moving on to the next control. Thankx

Edit: The question was answered: How to TAB through text fields in a ListView

+6
source share
3 answers

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)

+2
source

Set IsTabStop = "False" in the GridViewColumn and on the cell template. Now it should select only the TextBox inside the data template when the tab changes.

Try the following:

 <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> 
0
source

Source: https://habr.com/ru/post/894780/


All Articles