如何在 ListView 中通过文本框进行 TAB [英] How to TAB through TextBoxes in a ListView

查看:14
本文介绍了如何在 ListView 中通过文本框进行 TAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一个 ListView,它有 2 个 GridViewColumns,一个显示一个数字,一个包含一个 TextBox我的问题是我希望能够通过 GridViewColumn 中的所有 TextBox 进行 Tab.使用附加的 Property KeyboardNavigation.TabNavigation,我几乎实现了我想要的.
我实现的是:
第一个 TAB - 整个第一个 ListViewItem 集中
第二个 TAB - 第一个 TextBox 聚焦
第三个 TAB - 整个第二个 ListViewItem 聚焦
第四个 TAB - 第二个 TextBox 聚焦

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

我想要的是
第一个 TAB - 第一个 TextBox 焦点
第二个 TAB - 第二个 TextBox 聚焦

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

此代码是 H.B. 给我的..它应该在选择 ListViewÍtem 并找到 TextBox 并将其聚焦时执行.不知何故,它仍然不会每次都选择 TextBox,尽管执行此方法时 bool TextBoxgotFocus 始终为 true.

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

推荐答案

问题是对于列表视图中的每个项目,您有两个制表位:项目本身和文本框.您希望将项目本身的 KeyboardNavigation.IsTabStop 设置为 false.只需将其设置为您的项目样式即可.

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>

这篇关于如何在 ListView 中通过文本框进行 TAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆