wpf Infragistics XamDatagrid 绑定问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13393979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:13:29  来源:igfitidea点击:

Infragistics XamDatagrid binding issues

wpfdata-bindingmvvminfragisticsxamdatagrid

提问by Noich

I have an MVVM C#/WPF app. I'd like to bind a XamDataGrid to one of my view models, so that users can define how they want their results structure - what column is mapped to what variable.
My core holds such dictionary - Dictionary<string, string>, and I'd like to wrap it in an ObservableCollectionand bind to this, rather than copy the data to a DataTable. Problem is, I can't add rows this way (the view seems locked for additions) and changes are not saved. My code:

我有一个 MVVM C#​​/WPF 应用程序。我想将 XamDataGrid 绑定到我的一个视图模型,以便用户可以定义他们想要的结果结构 - 什么列映射到什么变量。
我的核心拥有这样的字典 - Dictionary<string, string>,我想将它包装在 an 中ObservableCollection并绑定到 this,而不是将数据复制到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>

the view model (relevant part):

视图模型(相关部分):

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

采纳答案by Noich

What worked eventually was using a BindingList. ObservableCollectionwon't let you add rows, but BindingListreally provides everything I need.

最终奏效的是使用BindingList. ObservableCollection不会让您添加行,但BindingList确实提供了我需要的一切。

回答by alhalama

For adding rows, the XamDataGrid uses either IEditableCollectionView.CanAddnew or IBindingList.AllowNew.

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

If you use a ListCollectionViewfor the DataSource of the grid then you can add new rows.

如果您对网格的数据源使用ListCollectionView,则可以添加新行。

To use the ListCollectionView with an ObservableCollection, pass the ObservableCollection into the ListCollectionView and then use the CollectionView to bind to the grid.

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