将焦点移到下一个单元格在DataGrid的WPF回车键按下? [英] Move Focus to Next Cell on Enter Key Press in WPF DataGrid?
本文介绍了将焦点移到下一个单元格在DataGrid的WPF回车键按下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想有一个自定义的DataGrid其中就可以了,
- 将下一单元格<大骨节病>输入骨节病>键时压也如果是在编辑模式。
- 在当前行中的最后一列到达,重点应转移到下一行的第一个单元格。
- 在延伸到下一个单元格,如果单元格为可编辑的,它会自动变成可编辑的。
- 如果单元格中包含
组合框
不comboboxcolumn,组合框应该DropDownOpen。
请帮我在这。我已经从过去几日通过创建一个自定义的DataGrid尝试,写了
一些代码 保护覆盖无效OnPreviewKeyDown(系统。 Windows.Input.KeyEventArgs E)
但我失败了。
解决方案
私人无效dg_PreviewKeyDown(对象发件人,发送KeyEventArgs E)
{
试
{
如果(e.Key == Key.Enter)
{
e.Handled = TRUE;
变种细胞= GetCell(dgIssuance,dgIssuance.Items.Count - 1,2);
如果(细胞!= NULL)
{
cell.IsSelected = TRUE;
cell.Focus();
dg.BeginEdit();
}
}
}
赶上(异常前)
{
的MessageBox(ex.Message,错误,MessageType.Error);
}
}
公共静态DataGridCell GetCell(DataGrid的DG,诠释行,诠释列)
{
VAR rowContainer = GETROW(DG,行) ;
如果(rowContainer!= NULL)
{
VAR主持人= GetVisualChild< DataGridCellsPresenter>(rowContainer);
如果(主持人!= NULL)
{
//尝试获取细胞,但它有可能是虚拟化的
细胞无功=(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(列);
如果(细胞== NULL)
{
//现在试图把眼帘从其获取细胞
dg.ScrollIntoView(rowContainer,dg.Columns [专栏]);
细胞=(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(列);
}
返回细胞;
}
}
返回NULL;
}
I want to have a Custom DataGrid which can,
- Move to next cell when Enter 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.
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)
But I failed.
解决方案
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;
}
这篇关于将焦点移到下一个单元格在DataGrid的WPF回车键按下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文