简体   繁体   English

Wpf datagrid输入键移动下一行

[英]Wpf datagrid Enter key move next row

I'm trying to develop a wpf app with a datagrid, and i want to allow the users to enter values like in excel. 我正在尝试使用datagrid开发一个wpf应用程序,我想让用户输入像excel这样的值。

Example: The datagrid has 2 columns, Name and BarCode The user is editing the BarCode on the first row, and when the user press Enter key, the focus should move the the row below on the BarCode cell. 示例:datagrid有2列,Name和BarCode用户正在编辑第一行的BarCode,当用户按Enter键时,焦点应移动BarCode单元格下面的行。

The user must be able to use a barcode scanner to register the barcodes, on the existing products list, without need to user the mouse or the keyboard. 用户必须能够使用条形码扫描仪在现有产品列表上注册条形码,而无需使用鼠标或键盘。

Any ideas on how to implement this behavior? 有关如何实现此行为的任何想法?

Thanks, Frederico 谢谢,弗雷德里科

A simpler solution is capture the KeyDown event and if the key is 'Enter', then move to the next tab. 一个更简单的解决方案是捕获KeyDown事件,如果键是'Enter',则移动到下一个选项卡。

private void dg_PreviewKeyDown(object sender, KeyEventArgs e)
{
    var u = e.OriginalSource as UIElement;
    if (e.Key == Key.Enter && u != null)
    {
        e.Handled = true;
        u.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
    }
}

I've just done this with some extra code, by handle the PreviewKeyUp event on the Datagrid, the working code is: 我刚刚用一些额外的代码完成了这个,通过处理Datagrid上的PreviewKeyUp事件,工作代码是:

private void DataGrid_PreviewKeyUp(object sender, KeyEventArgs e)
{
    if ((e.Key == Key.Enter) || (e.Key == Key.Return))
    {
        DataGrid grid = sender as DataGrid;
        if (grid.CurrentColumn.Header.ToString().Equals("Barcode", StringComparison.OrdinalIgnoreCase))
        {
            if (grid.SelectionUnit == DataGridSelectionUnit.Cell || grid.SelectionUnit == DataGridSelectionUnit.CellOrRowHeader)
            {
                var focusedElement = Keyboard.FocusedElement as UIElement;
                focusedElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
             }

             grid.BeginEdit();
             e.Handled = true;
        }
    }
}

The Column that hold the Barcode property is the only one i need for this behavior. 保存Barcode属性的列是我唯一需要此行为的列。

Something like this ? 像这样的东西?

//MainWindow.xaml.cs //MainWindow.xaml.cs

namespace BarcodeImplementationInDG
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<Products> lst = new List<Products>();
        public MainWindow()
        {
            InitializeComponent();
            dg.ItemsSource = lst;
        }
    }
    public class Products
    {
        public string Product { get; set; }
        public string Barcode { get; set; }
    }
}

//MainWindow.xaml //MainWindow.xaml

<Window x:Class="BarcodeImplementationInDG.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dg" VerticalAlignment="Top" HorizontalAlignment="Left" CanUserAddRows="True" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Height="309" Width="507" >
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="ProductColumn" Binding="{Binding Product}" Header="Product"  />
                <DataGridTextColumn x:Name="BarcodeColumn" Binding="{Binding Barcode}" Header="Barcode"  />
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>

The Data will be stored in the List lst , in case you want to use them 如果您想使用它们,数据将存储在List lst

On Enter, The focus automatically moves to the second row.. 在输入时,焦点自动移动到第二行..

If 3rd party WPF Grids are acceptable, Essential Grid can be used as it has this Excel like behavior built into the Grid. 如果第三方WPF网格是可接受的,可以使用Essential Grid,因为它具有内置于网格中的Excel行为。

The whole suite of controls is available for free through the community license program if you qualify. 如果您符合资格,可以通过社区许可计划免费获得整套控件。 Note: I work for Syncfusion. 注意:我为Syncfusion工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM