Wpf 数据网格 Enter 键移动下一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33292455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wpf datagrid Enter key move next row
提问by Frederico Regateiro
I'm trying to develop a wpf app with a datagrid, and i want to allow the users to enter values like in excel.
我正在尝试开发一个带有数据网格的 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
谢谢,弗雷德里科
回答by Ali Bayat
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));
}
}
回答by Frederico Regateiro
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 属性的 Column 是此行为唯一需要的。
回答by Ahmad Hammoud
Something like this ?
像这样的东西?
//MainWindow.xaml.cs
//主窗口.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
//主窗口.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..
回车后,焦点自动移到第二行。
回答by Davis Jebaraj
If 3rd party WPF Grids are acceptable, Essential Gridcan be used as it has this Excel like behavior built into the Grid.
如果可以接受第 3 方 WPF 网格,则可以使用Essential Grid,因为它具有内置于网格中的类似 Excel 的行为。
The whole suite of controls is available for free through the community licenseprogram if you qualify. Note: I work for Syncfusion.
如果您符合条件,可以通过社区许可计划免费获得整套控件。注意:我为 Syncfusion 工作。

