0

我有一个包含 5 列的 Datagrid。当我在第 2 列中更改网格中的单元格值时,我想在 Textbox_changed 事件中填充一些产品的列表视图。我的xml是:

<my:DataGrid Name="dgvSales" my:DataGridCell.Selected="DataGrid_GotFocus"  RowHeight="23" AlternatingRowBackground="LightGray" SelectionUnit="Cell"  BeginningEdit="dgvSales_BeginningEdit"  AutoGenerateColumns="False" Margin="13,150,9,117"  CellEditEnding="dgvSales_CellEditEnding" SelectionChanged="dgvSales_SelectionChanged" TargetUpdated="dgvSales_TargetUpdated" >
                   <my:DataGrid.Columns>
                        <my:DataGridTextColumn  Header="ItemID" Visibility="Hidden" Width="0" Binding="{Binding PList_Id}"></my:DataGridTextColumn>
                        <my:DataGridTextColumn  Header="Code" Width="100" Binding="{Binding Product_Code}"></my:DataGridTextColumn>
                        <my:DataGridTextColumn Header="Product Name" Width="200" Binding="{Binding Product_Name,NotifyOnTargetUpdated=False}">
                        <my:DataGridTextColumn.EditingElementStyle>
                            <Style TargetType="{x:Type TextBox}">
                                <EventSetter Event="TextChanged" Handler="TextBox_TextChanged" />
                            </Style>
                        </my:DataGridTextColumn.EditingElementStyle>
                        </my:DataGridTextColumn>
                       <my:DataGridTextColumn Header="Purchase Rate" Width="100" Binding="{Binding Purchase_Rate}"></my:DataGridTextColumn>
                        <my:DataGridTextColumn Header="Qty" Width="100" Binding="{Binding Qty}"></my:DataGridTextColumn>
                        <my:DataGridTextColumn Header="Amount" Width="100" Binding="{Binding Amount}"></my:DataGridTextColumn>
                    </my:DataGrid.Columns>
                </my:DataGrid>

我的代码隐藏是:

 private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox tb = (TextBox)sender;
        if (tb.Text.Trim() != "")
        {
            string qry = "select PM.Product_Code,PM.Product_Name,PM.Purchase_Rate   from dbo.Tbl_Product_Master PM   where PM.Product_Name like '%" + tb.Text.Trim() + "%' ";
            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;
                lstvwProductCode.Focus();
            }
            else
            {
                lstvwProductCode.ItemsSource = null;
                lstvwProductCode.Visibility = Visibility.Collapsed;
            }
        }
        else
        {
            lstvwProductCode.ItemsSource = null;
            lstvwProductCode.Visibility = Visibility.Collapsed;
        }
    }

我的问题是,当我在单元格上输入内容时,事件被触发,但我只得到第一个字符,并且我不能在单元格上键入多个字符,因为它无法编辑,如何使其可编辑并填充列表视图根据单元格中的文本如何执行此操作

编辑

我还使用了 DataGridTemplateColumn

 <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"></TextBox>
                                </DataTemplate>
                            </my:DataGridTemplateColumn.CellEditingTemplate>
                        </my:DataGridTemplateColumn>

我的活动是:

private void txtbxProduct_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox tb = (TextBox)sender;
        if (tb.Text.Trim() != "")
        {
           string qry = "select PM.Product_Code,PM.Product_Name,PM.Purchase_Rate   from dbo.Tbl_Product_Master PM   where PM.Product_Name like '%" + tb.Text.Trim() + "%' ";
            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;
                lstvwProductCode.Focus();
            }
            else
            {
                lstvwProductCode.ItemsSource = null;
                lstvwProductCode.Visibility = Visibility.Collapsed;
            }
        }
        else
        {
            lstvwProductCode.ItemsSource = null;
            lstvwProductCode.Visibility = Visibility.Collapsed;
        }
    }

它也不会帮助我

4

0 回答 0