如何使用TextBox作为TreeViewItems通过ListView进行TAB?

时间:2011-08-11 01:25:00

标签: wpf xaml listview textbox datatemplate

所以我基本上有这个ListView,我想按Tab并迭代我的TreeViewItems(最好只我的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>

我看到的情况是在第一次按下选项卡后选择了整个第一个TreeViewItem并再次按Tab键选择了第一个TextBox。最后,第三个TAB从TreeView中移出到下一个Control但是有更多TextBoxes,我想在“Tabing”到下一个Control之前捕获。 Thankx

编辑: 该问题在这里得到了解答: How to TAB through TextBoxes in a ListView

3 个答案:

答案 0 :(得分:5)

答案 1 :(得分:2)

也许我错过了一些东西,但我找不到任何简单的方法来做到这一点,这里将概述你能做什么:

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

我在这里做的事情:

  • 覆盖标签行为以触发命令以选择其他项目
  • 将事件处理程序添加到所选事件以聚焦TextBox
  • 命名TextBox,以便找到并集中注意力

代码:

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

这是非常粗略的,使用此任何部分需要您自担风险。 (聚焦也可以以不同的方式完成,而不会将选择带入我认为

Command类只是ICommand的通用实现,它接受一个在接口的Execute方法中执行的lambda)

答案 2 :(得分:0)

在GridViewColumn和Cell Template上设置IsTabStop =“False”。它现在应该只选择 选项卡更改中的数据模板内的TextBox。

试试这个:

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