在DataGrid WPF中捕获RowEdited事件

时间:2011-06-01 08:48:01

标签: wpf visual-studio-2010 .net-4.0 datagrid

如何在编辑DataGrid的行时捕获事件并从中获取所有值?

如果我使用RowEditEnding事件,我无法获得新值......

谢谢!

1 个答案:

答案 0 :(得分:0)

参见讨论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 });
        }
    }
}