TwoWay 数据绑定 dataGrid 和 dataTable

TwoWay data binding dataGrid and dataTable

我有一个数据表,其中包含来自 sql 数据库的一些数据。我想使用 mvvmcross 将此 dataTable 绑定到 dataGrid。这是我的代码:

模型视图中:

private DataTable _groupeData;
    public DataTable GroupeData
    {
        get { return _groupeData; }
        set
        {
            _groupeData = value;
            RaisePropertyChanged(() => GroupeData);
            Runned++;  //In order to check if the dataTable has updated
            UpdateElementGroupe(EltId, value);  //A method that will update the database using the values in this dataTable
        }

    }

视图中 :

<DataGrid ItemsSource="{Binding Path=GroupeData}" AutoGenerateColumns="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                            <DataGridTextColumn Header="Description" Binding="{Binding Description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                        </DataGrid.Columns>
                    </DataGrid>

使用这些代码,我可以在 DataGrid 中显示 dataTable 的数据,但是当我修改 dataGrid 中的单元格时,dataTable 不会更新(就像我设置了 Mode = OneWay 一样)。

问题已通过使用 event handler for the DataTable 解决,因此当一行发生更改时,事件处理程序会调用更新数据库的方法。

感谢grek40的帮助。

另一方面,使用 ObservableCellection 并不能解决问题,其中 T 是 class。但是如果你使用的是 ObservableCellection,我想你可以用同样的方式处理事件。