WPF:在 ItemsControl 中按 Enter 时从一个单元格移动到下一个单元格 [英] WPF: Moving from one cell to the next when pressing Enter in an ItemsControl

查看:43
本文介绍了WPF:在 ItemsControl 中按 Enter 时从一个单元格移动到下一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 控件定义如下:

I have a WPF control defined as follows:

        <ItemsControl ItemsSource="{Binding DutyValueBinders}" IsEnabled="{Binding Enabled}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Value, TargetNullValue=''}"  Width="50"></TextBox>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

这会生成一个网格,如下所示:

This produces a grid, looking like this:

因此,尽管 XAML 中只定义了一个文本框,但用户会看到一个网格.

So although there's only one text box defined in the XAML, the user sees a grid.

我想要做的是,在用户输入一个值并按下 Enter 或 Return 后,焦点应该传递到下一个单元格,依此类推,直到到达网格的末尾.我试着把这个例程放到后面的代码中(从 Stackoverflow 上的另一个线程复制):

What I want to do is, after the user has typed a value and pressed Enter or Return, focus should pass to the next cell and so on until the end of the grid is reached. I tried putting this routine into the code behind (copied from another thread on Stackoverflow):

    private void UserControl_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key != Key.Enter || (!e.IsToggled && sender is Button)) return;
        var request = new TraversalRequest(FocusNavigationDirection.Next);
        var keyboardFocus = Keyboard.FocusedElement as UIElement;

        if (keyboardFocus == null) return;
        keyboardFocus.MoveFocus(request);
        e.Handled = true;
    }

并修改 XAML 的标头如下:

and modified the header of the XAML as follows:

<UserControl ...
             (Other references)
             ... 
             d:DesignHeight="300" d:DesignWidth="300" PreviewKeyDown="UserControl_PreviewKeyDown">

问题在于,由于 XAML 定义中只有一个文本框,因此没有要遵循的 tabindex 顺序,因此当您按 Enter 时,光标就会消失.

The trouble is, as there's only one text box in the XAML definition, there's no tabindex order for it to follow, and so when you press Enter the cursor just vanishes.

除了完全废弃 XAML 并使用不同类型的控件重新开始之外,有什么方法可以做到这一点吗?

Is there any way to do this, short of scrapping the XAML entirely and starting again with a different type of control?

推荐答案

如何像这样处理 ItemsControlPreviewKeyDown 事件?:

How about handling the PreviewKeyDown event for the ItemsControl like this?:

<ItemsControl ItemsSource="{Binding DutyValueBinders}" IsEnabled="{Binding Enabled}"
              PreviewKeyDown="ItemsControl_PreviewKeyDown">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Value, TargetNullValue=''}"  Width="50"></TextBox>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

<小时>

private void ItemsControl_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        TextBox textBox = Keyboard.FocusedElement as TextBox;
        if (textBox != null)
        {
            textBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }
    }
}

这篇关于WPF:在 ItemsControl 中按 Enter 时从一个单元格移动到下一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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