gpt4 book ai didi

c# - 在 WPF DataGrid 中使用 Enter 键作为 Tab

转载 作者:太空狗 更新时间:2023-10-29 23:53:32 28 4
gpt4 key购买 nike

我在 WPF 中有一个 DataGrid

我想在按下 Enter 时移动到 NextCell,当到达 LastColumn 时,它应该具有创建或移动到下一行的默认 Enter 功能。

我不想使用Tab

我如何在 WPF 中执行此操作。

最佳答案

试试这个,我认为它至少对我有用。

//datagrid gotfocus event
private void dataGrid1_GotFocus(object sender, RoutedEventArgs e)
{
DependencyObject dep = (DependencyObject)e.OriginalSource;
//here we just find the cell got focused ...
//then we can use the cell key down or key up
// iteratively traverse the visual tree
while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
{
dep = VisualTreeHelper.GetParent(dep);
}

if (dep == null)
return;

if (dep is DataGridCell)
{
DataGridCell cell = dep as DataGridCell;
//raise key down event of cell
cell.IsSelected = true;
cell.KeyDown += new KeyEventHandler(cell_KeyDown);
}
}

void cell_KeyDown(object sender, KeyEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (e.Key == Key.Enter)
{
cell.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
cell.IsSelected = false;
e.Handled = true;
cell.KeyDown -= cell_KeyDown;
}
}

在这段代码中,当一个单元格获得焦点并且用户按下下一个单元格时,它将获得焦点。祝你好运,希望这对你有帮助。

编辑:

将此函数设置为数据网格 PreviewKeyDown 事件。

private void maindg_PreviewKeyDown(object sender, KeyEventArgs e)
{

//just accept enter key
if (e.Key != Key.Enter) return;

DependencyObject dep = (DependencyObject)e.OriginalSource;
//here we just find the cell got focused ...
//then we can use the cell key down or key up
// iteratively traverse the visual tree
while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
{
dep = VisualTreeHelper.GetParent(dep);
}

if (dep == null)
return;

if (dep is DataGridCell)
{
//cancel if datagrid in edit mode
maindg.CancelEdit();
//get current cell
DataGridCell cell = dep as DataGridCell;
//deselect current cell
cell.IsSelected = false;
//find next right cell
var nextCell = cell.PredictFocus(FocusNavigationDirection.Right);
//if next right cell null go for find next ro first cell
if (nextCell == null)
{
DependencyObject nextRowCell;
nextRowCell = cell.PredictFocus(FocusNavigationDirection.Down);
//if next row is null so we have no more row Return;
if (nextRowCell == null) return;
//we do this because we cant use FocusNavigationDirection.Next for function PredictFocus
//so we have to find it this way
while ((nextRowCell as DataGridCell).PredictFocus(FocusNavigationDirection.Left) != null)
nextRowCell = (nextRowCell as DataGridCell).PredictFocus(FocusNavigationDirection.Left);
//set new cell as next cell
nextCell = nextRowCell;
}
//change current cell
maindg.CurrentCell = new DataGridCellInfo(nextCell as DataGridCell);
//change selected cell
(nextCell as DataGridCell).IsSelected = true;
// start edit mode
maindg.BeginEdit();
}
//handl the default action of keydown
e.Handled = true;
}

关于c# - 在 WPF DataGrid 中使用 Enter 键作为 Tab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10287686/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com