繁体   English   中英

TwoWay 数据绑定 dataGrid 和 dataTable

[英]TwoWay data binding dataGrid and dataTable

我有一个 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)。

该问题已通过使用DataTable 的事件处理程序来解决,因此当更改行时,事件处理程序会调用更新数据库的方法。

感谢grek40的帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM