【问题标题】:C# WPF Select in DataGrid the first row with pressing Enter from a TextboxC# WPF 在 DataGrid 中选择第一行,从文本框中按 Enter
【发布时间】:2020-12-23 20:30:30
【问题描述】:

我有一个 TextBox (SearchBox - TabIndex=0) 和一个填充的 DataGrid TabIndex=1。 我想在 SearchBox 中输入关键字后按 Enter 选择 DataGrid 中的第一行。

到目前为止,我只有这个:

  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));
        }
    }

它选择整个 DataGrid 而不是行。

感谢您的帮助。

编辑: 我已将此添加到上面的代码中,问题已解决:-)

dataGrid.SelectedItem = dataGrid.Items[0];

【问题讨论】:

  • 请写下答案或删除问题。

标签: c# wpf datagrid keydown


【解决方案1】:

我已将以下代码添加到 Searchbox_KeyDown

dataGrid.SelectedItem = dataGrid.Items[0];

这解决了我的问题!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多