Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to TAB through TextBoxes in a ListView

Ok I have a ListView that has 2 GridViewColumns one displaying a number and one containing a TextBox My Problem is I want to be able to Tab through all the TextBoxes I have in the GridViewColumn. With the attached Property KeyboardNavigation.TabNavigation I achieve almost what i want.
What i achieve is :
first TAB - whole first ListViewItem focused
second TAB - first TextBox focused
third TAB - whole second ListViewItem focused
fourth TAB - second TextBox focused

What i want is
first TAB - first TextBox focused
second TAB - second TextBox focused

    <ListView KeyboardNavigation.TabNavigation="Continue" Name="TheLabelListView" >
                            <ListView.ItemContainerStyle >
                                    <EventSetter Event="Selected" Handler="ItemSelected" /></Style>
                            </ListView.ItemContainerStyle>
                            <ListView.View>
                                <GridView x:Name="GridViewSmall"  >
                                    <GridViewColumn  Header="#" Width="20"  DisplayMemberBinding="{Binding SelectorIndexNumber}" />
                                    <GridViewColumn  Header="Selector" Width="175">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBox Name="SelectorTextBox"  Text="{Binding SelectorName}"  />                                                    
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                </GridView>
                            </ListView.View>
                        </ListView>

This code was given to me by H.B. . It is supposed to execute when a ListViewÍtem is selected and finds the TextBox and focuses it. Somehow it still doesnt select the TextBox everytime allthough when this method is executed bool TextBoxgotFocus is always true.

 private void ItemSelected(object sender, RoutedEventArgs e)
    {
        var item = sender as ListViewItem;
        TextBox h = (FindNamedChild(item, "SelectorTextBox") as TextBox);
        bool TextBoxgotFocus = h.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;
    }
like image 865
M.C. Avatar asked Sep 18 '11 23:09

M.C.


1 Answers

The problem is that for each item in the list view, you have two tab stops: the item itself and the text box. You want to set KeyboardNavigation.IsTabStop to false for the items themselves. Just set that in your item's style.

<ListView KeyboardNavigation.TabNavigation="Continue" Name="TheLabelListView">
    <ListView.ItemContainerStyle>
        <Style>
            <Setter Property="KeyboardNavigation.IsTabStop" Value="False" />
        </Style>
    </ListView.ItemContainerStyle>

    <!-- etc... -->
</ListView>
like image 148
Jeff Mercado Avatar answered Sep 21 '22 07:09

Jeff Mercado