将数据导入DatagridComboBoxColumn不会更新组合列表值

时间:2016-08-23 18:43:08

标签: c# wpf excel mvvm datagridcomboboxcolumn

我构建了一个DataGrid,它包含一个自定义的ComboBoxColumn,当​​我用鼠标选择它时显示和选择正确的字段,但在ComboBox失去焦点后重置为列表中的第一个项目。此外,当我从Excel电子表格导入数据时,ComboBox不会更改以匹配工作表中的值。

注意:我计划重复使用此类为不同的数据创建一系列ComboBox。

什么阻止组合框接受新值?

ComboList类

namespace x.Models
{
  public class ComboList
  {
    public int FieldID {get;set;}
    public string FieldString {get;set;}
  }
}

XAML组合框代码

<DataGridComboBoxColumn Header="Platform Type" x:Name="PlatformTable" SelectedValueBinding="{Binding FieldID}" DisplayMemberPath="FieldString" SelectedValuePath="1" />

码(后面)

public partial class MainWindow: Window
{
  public ObservableCollection<Models.ComboList> PlatformCombo {get;set;}
  public MainWindow()
  {
    PlatformCombo = new ObservableCollection<Models.ComboList>()
    {
      new Models.ComboList() {FieldID=1,FieldString="Mounted"},
      new Models.ComboList() {FieldID=2,FieldString="Dismounted"}
    };
    PlatformTable.ItemsSource = PlatformCombo;
}

示例Excel数据

Platform Type
1
2

2 个答案:

答案 0 :(得分:0)

SelectedValueBinding更改为SelectedItemBinding会修复该字段上不接受新值的问题的一部分。 [link](DataGridComboBoxColumn not updating model WPF

我还有一个问题,就是为什么从Excel导入时,组合框永远不会更新;即使组合框有适当的数据,每行也会留空。

答案 1 :(得分:0)

必须将XAML组合框SelectedValueBinding设置为DataGrid组合框控件名称而不是通用组合框FieldID,并且UpdateSourceTrigger需要设置为LostFocus

SelectedValueBinding="{Binding Platform_Type, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"