我有一个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);
我不知道'XamDataGrid',但在普通的'DataGrid'中,你应该绑定到'ItemsSource'而不是'DataSource'。 – Rafal
'XamDataGrid'没有'ItemsSource'属性。 – Noich