我有一个 MVVM C#/WPF 应用程序。我想将 XamDataGrid 绑定到我的一个视图模型,以便用户可以定义他们想要的结果结构 - 将什么列映射到什么变量。
我的核心拥有这样的字典 - Dictionary<string, string>
,我想将它包装在一个ObservableCollection
并绑定到这个,而不是将数据复制到一个DataTable
. 问题是,我不能以这种方式添加行(视图似乎已锁定以进行添加)并且未保存更改。我的代码:
<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);