DataGrid中的ComboBox绑定问题与选择或新建项目 [英] DataGrid ComboBox Binding Issue with Selected or New Item

查看:272
本文介绍了DataGrid中的ComboBox绑定问题与选择或新建项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有保留在DataGridTemplate列组合框所选项目的问题。
我有DataTemplate中编辑ComboBox列DataGrid中的第一列,并在它旁边,我有一个文本列。该填充DataGrid与SQL存储过程读取数据。一切工作正常,除非我在组合框中选择一个项目,然后移动到文本字段,并开始它打字,组合选择空白的。它清空了两个新项目或现有的项目。奇怪的是,这种情况仅在第一次。当我重新选择组合框的值,或再次添加新项目,回到文本字段,它不是空白的。我运行的想法,并尝试了很多组合,但至今没有运气。
这里是我的code:

这是怎么了填充DataGrid:

 使用(CMD的SqlCommand =新的SqlCommand())
{
    cmd.CommandText =的GetProducts;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = sqlConn;    变种读者= cmd.ExecuteReader();
    VAR DT =新的DataTable();
    dt.Load(读卡器);
    dt.Columns [产品名称] AllowDBNull = TRUE。
    dtProductCfgTable = DT;
    ProductCfgGrid.ItemsSource = dtProductCfgTable.DefaultView;
}

这是ProductNamesList声明:

 公开名单<串GT; ProductNamesList {搞定;组; }

XAML:

 < D​​ataGridTemplateColumn标题=产品名称>
   < D​​ataGridTemplateColumn.CellTemplate>
      <&DataTemplate的GT;
        <组合框的ItemsSource ={结合ProductNamesList,
                                的RelativeSource = {的RelativeSource AncestorType =窗口}}
                                的SelectedItem ={结合产品名称
                                IsSynchronizedWithCurrentItem =假
                                了borderThickness =1.2 1.2 0 0BorderBrush =黑
                                背景=浅青绿IsEditable =真/>
      < / DataTemplate中>
   < /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
< D​​ataGridTextColumn绑定={结合ShippingAddress}
                    WIDTH =100
                    标题=Sh​​ippingAddress
                    能见度=可见/>


解决方案

中的数据丢失的原因是因为 CellTemplate 只提供非编辑功能,所以每当你编辑新行时改变了组合框的值,该数据没有得到设置,因为没有编辑模式的实施,使越来越被幕后创建任何对象。 DatagridTextColumn自动编辑已经建立了进去,这就是为什么组合框将编辑这种类型的细胞后工作。

 < D​​ataGridTemplateColumn标题=产品名称>
     < D​​ataGridTemplateColumn.CellTemplate>
         <&DataTemplate的GT;
                 <组合框的ItemsSource ={结合ProductNamesList,
                 的RelativeSource = {的RelativeSource AncestorType =窗口}}
                 的SelectedValue ={结合产品名称,模式=双向}
                 IsSynchronizedWithCurrentItem =假
                 IsEditable =假
                 IsHitTestVisible =FALSE/>
         < / DataTemplate中>
     < /DataGridTemplateColumn.CellTemplate>
     < D​​ataGridTemplateColumn.CellEditingTemplate>
         <&DataTemplate的GT;
             <组合框的ItemsSource ={结合ProductNamesList,
                 的RelativeSource = {的RelativeSource AncestorType =窗口}}
                 文本={结合产品名称,模式=双向}
                 IsSynchronizedWithCurrentItem =假
                 IsEditable =真/>
         < / DataTemplate中>
     < /DataGridTemplateColumn.CellEditingTemplate>
 < / DataGridTemplateColumn>

如果您希望用户看到一个组合框的非编辑模式时组合框的冗余才是必需的。如果你不关心,你可以简单的写:

 < D​​ataGridTemplateColumn.CellTemplate>
    <&DataTemplate的GT;
        < TextBlock的文本={结合产品名称}/>
    < / DataTemplate中>
 < /DataGridTemplateColumn.CellTemplate>

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>

这篇关于DataGrid中的ComboBox绑定问题与选择或新建项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆