我正在使用Telerik Datagrid For UWP。我正在使用PreviewKeyDown
来捕获Enter键。当按下Enter键时,我想将焦点设置为另一个控件。
private async void bookingGrid_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key==Windows.System.VirtualKey.Enter)
{
//the methodfires but focus does not shift to the required control
await FocusManager.TryFocusAsync(anotherControl, FocusState.Keyboard);
// this does not focus the other control??
}
}
其他键工作正常,但Enter键不工作。我试过
e.Handled = true;
和
e.Handled = false;
问题可能是您设置e.Handled = true
太晚了。
当达到await
时,系统的事件处理程序基本上停止执行,因此如果在await
之后设置Handled
,系统将不会拾取它,它将继续执行KeyDown
,这很可能会阻止焦点设置到另一个控件并将其保持在DataGrid
中。
您需要在焦点更改之前设置Handled
:
private async void bookingGrid_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key==Windows.System.VirtualKey.Enter)
{
e.Handled = true;
await FocusManager.TryFocusAsync(anotherControl, FocusState.Keyboard);
}
}
另一种解决方案是设置控制进程Enter键,并在KeyUp
事件处理程序中设置焦点。