按箭头键时文本框Keydown事件未触发

我有一个数据网格,其中一列作为DataGridTemplateColumn,如下所示:

                

当单元格值更改时,我想将一些项目填充到列表视图中,TextChanged事件如下:

  private void txtbxProduct_TextChanged(object sender, TextChangedEventArgs e) { TextBox tb = (TextBox)sender; if (tb.Text.Trim() != "") { string qry = "select PL.Record_Id as PList_Id,PM.Record_Id as Product_Id,PM.Product_Code,PM.Product_Name,PTM.Product_Type,PL.Purchase_Rate ,PL.Selling_Rate,PL.MRP from dbo.Tbl_Product_Master PM join Tbl_Product_List PL on PL.Product_Id=PM.Record_Id join Tbl_Product_Type_Master PTM on PTM.Record_Id=PM.Product_Category_Id where PL.Batch_Flag=0 and PM.Is_Del='false'and PM.Is_Active='true' and PM.Product_Name like '%" + tb.Text.Trim() + "%' order by PM.Product_Name "; DataSet ds = ObjCommon.GetObject.ExecuteQuery_Select(Connection.ConnectionString, qry); if (ds.Tables[0].Rows.Count > 0) { lstvwProductCode.ItemsSource = ds.Tables[0].DefaultView; lstvwProductCode.Visibility = Visibility.Visible; } else { lstvwProductCode.ItemsSource = null; lstvwProductCode.Visibility = Visibility.Collapsed; } } else { lstvwProductCode.ItemsSource = null; lstvwProductCode.Visibility = Visibility.Collapsed; } } 

当用户在键盘上输入向下键时,我想专注于列表视图但是当我按下向下,向后,空格等键时它不会触发Keydown事件,但是其他键如字母数字键都工作正常。我的keydown事件如下:

 private void txtbxProduct_KeyDown(object sender, KeyEventArgs e) { TextBox tb = (TextBox)sender; if (e.Key == Key.Escape) { tb.Clear(); lstvwProductCode.Visibility = Visibility.Collapsed; tb.Focus(); } else if (e.Key == Key.Down) { if (lstvwProductCode.Items.Count > 0) { lstvwProductCode.SelectedIndex = 0; lstvwProductCode.Focus(); lstvwProductCode.Visibility = Visibility.Visible; } } } 

我在代码中做错了什么?

而不是使用PreviewKeyDown

对于通常由WPF处理但是PreviewKeyDown事件的导航键,不会引发KeyDown事件。 如果您不希望WPF也处理键事件,则应设置Handled=True