C# 在WPF DataGrid中按Enter键时移动到下一列
C# 在WPF DataGrid中按Enter键时移动到下一列,c#,wpf,datagrid,wpfdatagrid,C#,Wpf,Datagrid,Wpfdatagrid,DataGrid的默认行为是按下Enter键时向下移动。我正在尝试将其更改为移动到右侧 我已经看到并尝试实现它,如下所示。这是一种工作方式,当按下Enter键时,它会向右移动一列,但也会向下移动一行 例如,当我在包含55的单元格上并按enter键时,我会在包含20的单元格上结束 我已经调试过了,但似乎没有找到问题所在单元格的值在cell.Focus()中是正确的我不知道这之后会发生什么。奇怪的是,当我在最后一行并按enter键时,它会工作 ------------------ | Depth |
DataGrid
的默认行为是按下Enter
键时向下移动。我正在尝试将其更改为移动到右侧
我已经看到并尝试实现它,如下所示。这是一种工作方式,当按下Enter
键时,它会向右移动一列,但也会向下移动一行
例如,当我在包含55
的单元格上并按enter键时,我会在包含20
的单元格上结束
我已经调试过了,但似乎没有找到问题所在单元格的值在cell.Focus()中是正确的
我不知道这之后会发生什么。奇怪的是,当我在最后一行并按enter键时,它会工作
------------------
| Depth | Width |
------------------
| 55 | 30 |
------------------
| 45 | 20 |
------------------
公共静态void SelectCellByIndex(DataGrid DataGrid,int rowIndex,int columnIndex)
{
如果(!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.CellorRoroWheader))
抛出新ArgumentException(“DataGrid的SelectionUnit必须设置为Cell。”);
if(rowIndex<0 | | rowIndex>(dataGrid.Items.Count-1))
抛出新ArgumentException(string.Format(“{0}是无效的行索引。”,rowIndex));
if(columnIndex<0 | | columnIndex>(dataGrid.Columns.Count-1))
抛出新ArgumentException(string.Format(“{0}是无效的列索引。”,columnIndex));
dataGrid.SelectedCells.Clear();
对象项=dataGrid.Items[rowIndex];//=产品X
DataGridRow行=dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex)作为DataGridRow;
if(行==null)
{
dataGrid.ScrollingToView(项目);
行=dataGrid.ItemContainerGenerator.ContainerFromIndex(行索引)作为DataGridRow;
}
如果(行!=null)
{
DataGridCell=GetCell(dataGrid,行,列索引);
如果(单元格!=null)
{
DataGridCellInfo DataGridCellInfo=新的DataGridCellInfo(单元格);
dataGrid.SelectedCells.Add(dataGridCellInfo);
cell.Focus();
}
}
}
公共静态DataGridCell GetCell(DataGrid DataGrid,DataGridRow rowContainer,int列) { if(rowContainer!=null) { DataGridCellsPresenter=FindVisualChild(rowContainer); 如果(演示者==null) { /*如果行已被虚拟化,请调用其ApplyTemplate()方法 *为DataGridCellsPresenter构建其可视化树 *以及要创建的DataGridCell*/ rowContainer.ApplyTemplate(); 演示者=FindVisualChild(rowContainer); } 如果(演示者!=null) { DataGridCell=presenter.ItemContainerGenerator.ContainerFromIndex(列)作为DataGridCell; if(单元格==null) { /*将该列显示在视图中 *以防它被虚拟化了*/ ScrollIntoView(rowContainer,dataGrid.Columns[column]); 单元格=presenter.ItemContainerGenerator.ContainerFromIndex(列)作为DataGridCell; } 返回单元; } } 返回null; }
public static T FindVisualChild(DependencyObject obj),其中T:DependencyObject
{
for(int i=0;i
原因可能是,一旦完成,事件将继续进行正常过程,并在某个地方完成
您要做的是在方法的末尾,在处理完它之后,将handled
设置为true
// do your stuff
e.Handled = true;
// return here, and it should not go down as well
哇!我怀疑这与它运行了好几次有关,但我不知道如何处理它!多谢!没问题,伙计。很高兴这有帮助:)
public static void SelectCellByIndex(DataGrid dataGrid, int rowIndex, int columnIndex)
{
if (!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.CellOrRowHeader))
throw new ArgumentException("The SelectionUnit of the DataGrid must be set to Cell.");
if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1))
throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex));
if (columnIndex < 0 || columnIndex > (dataGrid.Columns.Count - 1))
throw new ArgumentException(string.Format("{0} is an invalid column index.", columnIndex));
dataGrid.SelectedCells.Clear();
object item = dataGrid.Items[rowIndex]; //=Product X
DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
if (row == null)
{
dataGrid.ScrollIntoView(item);
row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
}
if (row != null)
{
DataGridCell cell = GetCell(dataGrid, row, columnIndex);
if (cell != null)
{
DataGridCellInfo dataGridCellInfo = new DataGridCellInfo(cell);
dataGrid.SelectedCells.Add(dataGridCellInfo);
cell.Focus();
}
}
}
public static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column)
{
if (rowContainer != null)
{
DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer);
if (presenter == null)
{
/* if the row has been virtualized away, call its ApplyTemplate() method
* to build its visual tree in order for the DataGridCellsPresenter
* and the DataGridCells to be created */
rowContainer.ApplyTemplate();
presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer);
}
if (presenter != null)
{
DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
if (cell == null)
{
/* bring the column into view
* in case it has been virtualized away */
dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
}
return cell;
}
}
return null;
}
public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
return (T)child;
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
// do your stuff
e.Handled = true;
// return here, and it should not go down as well