wpf如何实现按钮的回车事件_c# – 在WPF DataGrid中使用Enter键作为选项卡

本文介绍如何在WPF DataGrid中利用Enter键作为Tab键进行导航。通过监听DataGrid的GotFocus和PreviewKeyDown事件,以及遍历视觉树找到当前聚焦的单元格,当用户按下Enter键时,焦点会移动到下一个单元格,实现类似选项卡的效果。代码示例详细展示了事件处理过程。
摘要由CSDN通过智能技术生成

尝试这个我觉得它起作用至少对我有用.

//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;

}

}

在此代码中,当一个单元格获得焦点并且用户键关闭时,下一个单元格将获得焦点.

祝你好运,希望这能帮助你.

编辑:

将此函数设置为datagrid 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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值