wpf - 所选项目或新项目的 DataGrid ComboBox 绑定(bind)问题

标签 wpf binding datagrid combobox

我在将所选项目保留在 DataGridTemplate 列组合框中时遇到问题。 我将 DataTemplate 可编辑组合框列作为数据网格中的第一列,在它旁边,我有一个文本列。 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; }

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

最佳答案

数据丢失的原因是因为 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>

关于wpf - 所选项目或新项目的 DataGrid ComboBox 绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18061176/

相关文章:

c# - View 中的多个数据上下文

.net - WPF 数据网格和动态列宽

WPF DataGrid 绑定(bind)到数据表

c# - 数据网格列标题按钮

wpf - 如何让 ViewModel 意识到转换错误

c# - 检查应用程序是否发出声音

c# - 如何使用 MVVM 绑定(bind)多个数据网格源?

wpf - 如何将两个 View 与两个 viemodel 绑定(bind)在一起我 wpf?

c# - 将一个对象复制到另一个对象

wpf - 如何在 WPF 中将字符串绑定(bind)为 double?