[英]C# WPF Select in DataGrid the first row with pressing Enter from a Textbox
I have a TextBox (SearchBox - TabIndex=0) and a populated DataGrid TabIndex=1.我有一个 TextBox (SearchBox - TabIndex=0) 和一个填充的 DataGrid TabIndex=1。 I want after input of the keyword in the SearchBox by pressing Enter to select the first row in the DataGrid.
我想在 SearchBox 中输入关键字后按 Enter 到 DataGrid 中的第一行 select。
So far i have just this:到目前为止,我只有这个:
private void txtSearchBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter)
{
System.Windows.Controls.TextBox txtb = sender as System.Windows.Controls.TextBox;
txtb.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
It selects the whole DataGrid and not the row.它选择整个 DataGrid 而不是行。
Thank you for your help.谢谢您的帮助。
EDIT: I´ve added this to code above and the problem was solved:-)编辑:我将此添加到上面的代码中,问题已解决:-)
dataGrid.SelectedItem = dataGrid.Items[0];
I´ve added the following code to the Searchbox_KeyDown我将以下代码添加到 Searchbox_KeyDown
dataGrid.SelectedItem = dataGrid.Items[0];
And this solved my problem!这解决了我的问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.