2012-11-15 40 views
1

我有一个MVVM C#/ WPF应用程序。我想将XamDataGrid绑定到我的视图模型之一,以便用户可以定义他们希望如何使用他们的结果结构 - 哪个列映射到哪个变量。
我的核心拥有这样的词典 - Dictionary<string, string>,我想将它包装在ObservableCollection中并绑定到此,而不是将数据复制到DataTable。问题是,我不能以这种方式添加行(视图似乎锁定添加),并且更改不会保存。我的代码:Infragistics XamDatagrid绑定问题

<igDP:XamDataGrid 
     Grid.Row="1" Grid.Column="0" 
     Name="resultStructure" 
     DataSource="{Binding VariablesDictionary, Mode=TwoWay}" 
     GroupByAreaLocation="None"> 
     <igDP:XamDataGrid.FieldLayoutSettings> 
      <igDP:FieldLayoutSettings 
       AllowFieldMoving="WithinLogicalRow" 
       AllowAddNew="True" 
       AllowDelete="True" 
       AddNewRecordLocation="OnBottomFixed" /> 
     </igDP:XamDataGrid.FieldLayoutSettings> 
    </igDP:XamDataGrid> 

视图模型(相关部分):

private ObservableCollection<DictionaryEntry> variablesDictionary; 
     public ObservableCollection<DictionaryEntry> VariablesDictionary 
     { 
      get { return variablesDictionary; } 
      set 
      { 
       variablesDictionary = value; 
       OnPropertyChanged(()=>VariablesDictionary); 
      } 
     } 

...

List<DictionaryEntry> vars = resultStructureModel.Variables.Select(x => new DictionaryEntry {Key = x.Key, Value = x.Value}).ToList(); 
      VariablesDictionary = new ObservableCollection<DictionaryEntry>(vars); 
+0

我不知道'XamDataGrid',但在普通的'DataGrid'中,你应该绑定到'ItemsSource'而不是'DataSource'。 – Rafal

+0

'XamDataGrid'没有'ItemsSource'属性。 – Noich

回答

0

最终有效的是使用BindingListObservableCollection不会让你添加行,但BindingList确实提供了我需要的一切。

1

对于添加行,在XamDataGrid使用或者IEditableCollectionView .CanAddnew或IBindingList.AllowNew 。

如果您使用ListCollectionView作为网格的数据源,那么您可以添加新行。

要将ListCollectionView与ObservableCollection一起使用,请将ObservableCollection传递到ListCollectionView,然后使用CollectionView绑定到网格。

相关问题