I am having issue with retaining selected item in a DataGridTemplate column ComboBox. I have the DataTemplate editable combobox column as the first column in the datagrid and next to it, I have a text column. The DataGrid is populated with data read from SQL stored procedure. Everything works fine, except when I select an item in the combobox and move to the text field and start typing in it, the Combo selection blanks out. It blanks out both for a new item or existing item. Oddly, this happens only the first time. When I reselect the ComboBox Value or add the new item again and go back to the text field, it does not blank out. I am running out of ideas and tried many combinations, but no luck so far. Here is my code:
This is how I am populating the DataGrid:
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "GetProducts";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConn;
var reader = cmd.ExecuteReader();
var dt = new DataTable();
dt.Load(reader);
dt.Columns["ProductName"].AllowDBNull = true;
dtProductCfgTable = dt;
ProductCfgGrid.ItemsSource = dtProductCfgTable.DefaultView;
}
This is the declaration for ProductNamesList:
public List<string> ProductNamesList { get; set; }
XAML:
<DataGridTemplateColumn Header="ProductName">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ProductNamesList,
RelativeSource={RelativeSource AncestorType=Window}}"
SelectedItem="{Binding ProductName
IsSynchronizedWithCurrentItem="False"
BorderThickness="1.2 1.2 0 0" BorderBrush="Black"
Background="LightCyan" IsEditable="True" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding ShippingAddress}"
Width="100"
Header="ShippingAddress"
Visibility="Visible"/>
The reason the data is lost is because the CellTemplate
only provides non-edit features, so whenever you changed a value in the combobox when editing a new row, the data wasn't getting set because there was no edit-mode implementation, so no object was getting created behind the scenes. DatagridTextColumn automatically has editing build into it, which is why the combobox would work after editing this type of cell.
<DataGridTemplateColumn Header="ProductName" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ProductNamesList,
RelativeSource={RelativeSource AncestorType=Window}}"
SelectedValue="{Binding ProductName, Mode=TwoWay}"
IsSynchronizedWithCurrentItem="False"
IsEditable="False"
IsHitTestVisible="False" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ProductNamesList,
RelativeSource={RelativeSource AncestorType=Window}}"
Text="{Binding ProductName, Mode=TwoWay}"
IsSynchronizedWithCurrentItem="False"
IsEditable="True" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
The redundancy in comboboxes is only necessary if you want the user to see a combobox when in non-edit mode. If you don't care about that you can simply write:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProductName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments