本文介绍了在 WPF DataGrid 中按下 Enter 键时将焦点移到下一个单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个可以的自定义 DataGrid,
I want to have a Custom DataGrid which can,
- 如果处于编辑模式,则在按下 键时移动到下一个单元格.
- 当到达当前行的最后一列时,焦点应移动到下一行的第一个单元格.
- 到达下一个单元格时,如果该单元格是可编辑的,它应该自动变为可编辑.
- 如果单元格包含
ComboBox
而不是组合框列,组合框应该 DropDownOpen.
- Move to next cell when key is pressed also if it is in edit mode.
- When the last column in the current row is reach, the focus should move to the first cell of next row.
- On reaching to next cell, if the cell is editable, it should automatically became editable.
- If the cell contains an
ComboBox
not comboboxcolumn, the combobox should DropDownOpen.
请帮我解决这个问题.过去几天我一直在尝试创建自定义 DataGrid 并在
Please help me in this. I have been trying from the past few day by creating a Custom DataGrid and wrote some code in
protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
但我失败了.
推荐答案
private void dg_PreviewKeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.Enter)
{
e.Handled = true;
var cell = GetCell(dgIssuance, dgIssuance.Items.Count - 1, 2);
if (cell != null)
{
cell.IsSelected = true;
cell.Focus();
dg.BeginEdit();
}
}
}
catch (Exception ex)
{
MessageBox(ex.Message, "Error", MessageType.Error);
}
}
public static DataGridCell GetCell(DataGrid dg, int row, int column)
{
var rowContainer = GetRow(dg, row);
if (rowContainer != null)
{
var presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
if (presenter != null)
{
// try to get the cell but it may possibly be virtualized
var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
// now try to bring into view and retreive the cell
dg.ScrollIntoView(rowContainer, dg.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
}
return null;
}
这篇关于在 WPF DataGrid 中按下 Enter 键时将焦点移到下一个单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!