C# 更改键盘输入的默认行为
我想禁用WPFC# 更改键盘输入的默认行为,c#,wpf,datagrid,wpfdatagrid,C#,Wpf,Datagrid,Wpfdatagrid,我想禁用WPFDataGridUI控件的默认行为,即在特定单元格上按enter按钮时,焦点自动移动到下一个单元格。它应该只提交已编辑的新数据,而不移动到下一个单元格 我通过安装一个PreviewKeyDown处理程序并使用两个MoveFocus调用找到了解决方法。如果没有该解决方法(仅使用e.Handled=true语句),编辑的数据将无法正确提交(单元格将无限期地处于编辑模式) XAML: 有人能帮我找到更好的解决方案吗?调用committedit()方法提交数据: private void
DataGrid
UI控件的默认行为,即在特定单元格上按enter按钮时,焦点自动移动到下一个单元格。它应该只提交已编辑的新数据,而不移动到下一个单元格
我通过安装一个PreviewKeyDown
处理程序并使用两个MoveFocus
调用找到了解决方法。如果没有该解决方法(仅使用e.Handled=true
语句),编辑的数据将无法正确提交(单元格将无限期地处于编辑模式)
XAML:
有人能帮我找到更好的解决方案吗?调用committedit()
方法提交数据:
private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
e.Handled = true;
((DataGrid)sender).CommitEdit();
}
}
private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
var uiElement = e.OriginalSource as UIElement;
if (e.Key == Key.Enter && uiElement != null)
{
e.Handled = true;
uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
}
}
private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
e.Handled = true;
((DataGrid)sender).CommitEdit();
}
}