使用键盘箭头在DataGrid中导航

时间:2019-07-19 10:01:49

标签: c# wpf datagrid keydown keyeventargs

要求是使用向右和向左箭头键水平浏览DataGrid。

我尝试获取行和列索引,然后获取DataGridRow,然后获取DataGridCell。这样,我就能得到下一个单元格。但这是交换整个列

if (e.Key == Key.Right)
            {
                DataGridCellInfo cellinfo = this.dataGrid1.SelectedCells;
                int ColCount = dataGrid1.Columns.Count;

                int SelectedCellCount = cellinfo.Count;
                DataGridCellInfo cell = dataGrid1.CurrentCell;
                RowColTuple = RowColumnIndex(cell);
                int rowIndex = RowColTuple.Item1;
                int columnindex = RowColTuple.Item2;

                if (columnindex < ColCount && rowIndex < dataGrid1.Items.Count)
                {
                    DataGridRow Row = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(rowIndex);
                    DataGridCell NextRowColumnCell = dataGrid1.Columns[columnindex].GetCellContent(Row).Parent as DataGridCell;
                    NextRowColumnCell.IsSelected = true;
                }
                if (columnindex == ColCount)
                {
                    columnindex = 0;
                    DataGridRow Row = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(rowIndex + 1);
                    DataGridCell NextRowColumnCell = dataGrid1.Columns[columnindex].GetCellContent(Row).Parent as DataGridCell;
                    NextRowColumnCell.IsSelected = true;
                }

期望使用左右箭头键在整个DataGrid中进行水平导航。

0 个答案:

没有答案