2016-08-23 54 views
0

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

注:我打算重用这个类来为不同的数据创建一系列ComboBoxes。

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

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

我已经加入UpdateSourceTrigger和模式的SelectedValueBinding,但我看到了同样的问题: 'SelectedValueBinding = “{结合FieldID,UpdateSourceTrigger =的PropertyChanged,模式=双向}”' – Kahlan

回答

0

的XAML组合框SelectedValueBinding必须设置到DataGrid组合框控件名称,而不是通用组合框FieldIDUpdateSourceTrigger需要被设置为LostFocus

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

0

SelectedValueBinding更改为SelectedItemBinding可以解决现场不接受新值的问题的一部分。 [链接](DataGridComboBoxColumn not updating model WPF

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