How to TAB through TextBoxes in a ListView

烂漫一生 提交于 2020-01-21 01:12:07

问题


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;
    }

回答1:


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>


来源:https://stackoverflow.com/questions/7465151/how-to-tab-through-textboxes-in-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!