我有一个包含5列的Datagrid。当我在第2列中更改网格中的单元格值时,我想在Textbox_changed事件中使用某些产品填充listview。 我的xaml是:
<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;
}
}
我的问题是,当我在单元格上输入事件被触发但我只获得第一个字符时,我不能在单元格上键入多个字符,因为它不可编辑,如何使其可编辑和根据单元格中的文本填充listview如何执行此操作
修改
我还使用了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;
}
}
它也不会帮助我