当箭头键按下时,文本框键控事件不会触发 [英] Textbox Keydown event not firing when arrow key press
本文介绍了当箭头键按下时,文本框键控事件不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< my:DataGrid Name =dgvSalesRowHeight =23SelectionUnit =CellBeginningEdit =dgvSales_BeginningEditAutoGenerateColumns =FalseCellEditEnding =dgvSales_CellEditEnding>
< my:DataGrid.Columns>
< my:DataGridTemplateColumn Header =Product NameWidth =200>
< my:DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< TextBlock Text ={Binding Product_Name}>< / TextBlock>
< / DataTemplate>
< / my:DataGridTemplateColumn.CellTemplate>
< my:DataGridTemplateColumn.CellEditingTemplate>
< DataTemplate>
< TextBox x:Name =txtbxProductText ={Binding Product_Name}TextChanged =txtbxProduct_TextChangedKeyDown =txtbxProduct_KeyDown>< / TextBox>
< / DataTemplate>
< / my:DataGridTemplateColumn.CellEditingTemplate>
< / my:DataGridTemplateColumn>
< / my:DataGrid.Columns>
< / my:DataGrid>
当单元格值更改时,我想将一些项目填充到列表视图中,TextChanged事件如下:
private void txtbxProduct_TextChanged(object sender,TextChangedEventArgs e)
{
TextBox tb =(TextBox)发件人
if(tb.Text.Trim()!=)
{
string qry =选择PL.Record_Id作为PList_Id,PM.Record_Id作为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加入Tbl_Product_List PL PL.Product_Id = PM.Record_Id在PTM上连接Tbl_Product_Type_Master PTM.Record_Id = PM.Product_Category_Id其中PL.Batch_Flag = 0和PM.Is_Del ='false'和PM.Is_Active ='true'和PM.Product_Name像'%'+ tb.Text.Trim()+%由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;
}
}
当用户输入键盘上的向下键时想要专注于listview,但是当我按Down,Back,Space等键时,它不会触发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
I have a datagrid with one column as DataGridTemplateColumn as follows :
<my:DataGrid Name="dgvSales" RowHeight="23" SelectionUnit="Cell" BeginningEdit="dgvSales_BeginningEdit" AutoGenerateColumns="False" CellEditEnding="dgvSales_CellEditEnding" >
<my:DataGrid.Columns>
<my:DataGridTemplateColumn Header="Product Name" Width="200">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Product_Name}"></TextBlock>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
<my:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox x:Name="txtbxProduct" Text="{Binding Product_Name}" TextChanged="txtbxProduct_TextChanged" KeyDown="txtbxProduct_KeyDown"></TextBox>
</DataTemplate>
</my:DataGridTemplateColumn.CellEditingTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>
When the cell value change I want to populate some items to the listview ,the TextChanged event is as follows :
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;
}
}
When the User enter the down key on the keyboard I want to focus on the listview but It won't fire the Keydown event when I press Down,Back,Space etc keys ,But other keys like alphanumeric keys all working fine .My keydown event is as follows :
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;
}
}
}
What I did wrong in my code ?
解决方案
Instead of that Use PreviewKeyDown
这篇关于当箭头键按下时,文本框键控事件不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文