如何在 ListView 中的文本框上按 TAB 键

发布于 2024-12-05 03:44:48 字数 2743 浏览 1 评论 0原文

好的,我有一个 ListView,它有 2 个 GridViewColumns,一个显示数字,一个包含文本框 我的问题是我希望能够通过 Tab 键浏览 GridViewColumn 中的所有文本框。 通过附加的属性 KeyboardNavigation.TabNavigation 我几乎实现了我想要的。
我取得的成就是:
第一个 TAB - 整个第一个 ListViewItem 聚焦
第二个 TAB - 第一个文本框聚焦
第三个 TAB - 整个第二个 ListViewItem 聚焦
第四个选项卡 - 第二个文本框聚焦

我想要的是
第一个 TAB - 第一个文本框聚焦
第二个 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>

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气称霸 2024-12-12 03:44:48

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

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

    <!-- etc... -->
</ListView>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文