Wpf 选定项或新项的DataGrid ComboBox绑定问题

Wpf 选定项或新项的DataGrid ComboBox绑定问题,wpf,binding,datagrid,combobox,Wpf,Binding,Datagrid,Combobox,我在DataGridTemplate列组合框中保留选定项时遇到问题。 DataTemplate editable combobox列作为datagrid中的第一列,在它旁边有一个文本列。DataGrid由从SQL存储过程读取的数据填充。一切正常,除了在组合框中选择一个项目并移动到文本字段并开始在其中键入时,组合选择将清空。对于新项目或现有项目,它都将清空。奇怪的是,这只是第一次发生。当我重新选择组合框值或再次添加新项并返回文本字段时,它不会空白。我已经没有主意了,尝试了很多组合,但到目前为止运气

我在DataGridTemplate列组合框中保留选定项时遇到问题。 DataTemplate editable combobox列作为datagrid中的第一列,在它旁边有一个文本列。DataGrid由从SQL存储过程读取的数据填充。一切正常,除了在组合框中选择一个项目并移动到文本字段并开始在其中键入时,组合选择将清空。对于新项目或现有项目,它都将清空。奇怪的是,这只是第一次发生。当我重新选择组合框值或再次添加新项并返回文本字段时,它不会空白。我已经没有主意了,尝试了很多组合,但到目前为止运气不好。 这是我的密码:

这就是我填充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;
}
这是ProductNamesList的声明:

public List<string> ProductNamesList { get; set; }
public List ProductNamesList{get;set;}
XAML:



数据丢失的原因是,
CellTemplate
仅提供非编辑功能,因此,每当您在编辑新行时更改组合框中的值时,都不会设置数据,因为没有实现编辑模式,因此不会在幕后创建对象。DatagridTextColumn自动将编辑内置于其中,这就是为什么组合框在编辑这种类型的单元格后可以工作的原因

<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>

只有当您希望用户在非编辑模式下看到组合框时,组合框中的冗余才是必要的。如果你不在乎,你可以写:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding ProductName}" />
    </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>


您将datacontext设置为什么?在这种情况下,我没有为网格设置任何datacontext查看并尝试这些解决方案尝试过,但运气不佳。这是一个很好的答案。谢谢然而,虽然这对组合列表中的现有项目非常有效,但我无法通过它添加新项目。有什么想法吗?只需将
SelectedValue
更改为
Text
。这将导致Combobox的textbox控件获取列表中未包含的值。但是请注意,添加您自己的值不会导致它们进入列表。为了实现此功能,您必须为组合框编写事件处理程序,例如
LostFocus
。当我将其更改为SelectedValue时,它会再次丢失数据。但我留下这个作为答案,因为我可以不添加新记录。感谢您的帮助我的投票是因为ElementStyle with ItemSource,SelectedValue而编辑ElementStyle with ItemSource,Text
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding ProductName}" />
    </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>