我一直在尝试用谷歌搜索这个,但一直无法找到适合我的解决方案。
我有一个 DataGrid,它显示来自 SQL 表的一些客户端不知道的信息。
客户端仅向服务器发送请求并获得 List
我需要检测用户何时对一行进行了更改,并且我需要用户输入的新值。 目前我正在使用 RowEditEnding 事件。然后处理此事件的方法可以:
private void editRowEventHandler(object sender, DataGridRowEditEndingEventArgs e)
{
SomeClass sClass = e.Row.DataContext as SomeClass;
// Send sClass to the server to be saved in the database...
}
这给了我正在编辑的行。但它给了我更改前的行,而我无法弄清楚如何在更改发生后获取该行。
这里有没有人知道我该怎么做,或者可以指出我可以找到的方向?
最佳答案
查看讨论 here , 以避免逐个单元地读取。
private void OnRowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;
if (e.EditAction == DataGridEditAction.Commit) {
ListCollectionView view = CollectionViewSource.GetDefaultView(dataGrid.ItemsSource) as ListCollectionView;
if (view.IsAddingNew || view.IsEditingItem) {
this.Dispatcher.BeginInvoke(new DispatcherOperationCallback(param =>
{
// This callback will be called after the CollectionView
// has pushed the changes back to the DataGrid.ItemSource.
// Write code here to save the data to the database.
return null;
}), DispatcherPriority.Background, new object[] { null });
}
}
}
关于wpf - 检测何时在 DataGrid 中编辑行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8607856/