Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

c# - Textbox Keydown event not firing when arrow key press

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of that Use PreviewKeyDown


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...