简体   繁体   English

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

[英]Importing data into DatagridComboBoxColumn does not update combo list value

I build a DataGrid the contains a custom ComboBoxColumn that shows and selects the proper fields when I select it with a mouse, but resets to the first item in the list after the ComboBox loses focus. 我构建了一个DataGrid,其中包含一个自定义ComboBoxColumn,当​​我用鼠标选择它时会显示并选择适当的字段,但是在ComboBox失去焦点后会重置为列表中的第一项。 Additionally, when when I import data from an Excel spreadsheet, the ComboBox does not change to match the value in the worksheet. 此外,当我从Excel电子表格导入数据时,ComboBox不会更改以匹配工作表中的值。

Note: I'm planning on reusing this class to create a series of ComboBoxes for different data. 注意:我打算重用该类为不同的数据创建一系列ComboBox。

What is preventing the Combo Box from accepting a new value? 是什么导致组合框无法接受新值?

ComboList class 组合列表类

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

XAML Combo Box code XAML组合框代码

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

Code(behind) 代码(后面)

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

Sample Excel data 样本Excel数据

Platform Type
1
2

Changing SelectedValueBinding to SelectedItemBinding fixes one part of the problem on the field not accepting a new value. SelectedValueBinding更改为SelectedItemBinding解决部分字段不接受新值的问题。 [link] ( DataGridComboBoxColumn not updating model WPF ) [link]( DataGridComboBoxColumn不更新模型WPF

I'm still left with the question as to why when importing from Excel, the combo box is never updated; 我还有一个问题,为什么从Excel导入时,组合框永远不会更新;为什么? left empty for each row, even though there is appropriate data for the combo box. 即使组合框有适当的数据,每一行也都留空。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM