开发者

WPF DataGrid has RowEditEnding but no RowEditEnded

开发者 https://www.devze.com 2023-01-19 07:38 出处:网络
I\'ve bound an ObservableCollection to a DataGrid. When I change values in the DataGrid, the RowEditEnding event is raised. But the e.Row.Item is the object before editing, so you don\'t see the new v

I've bound an ObservableCollection to a DataGrid. When I change values in the DataGrid, the RowEditEnding event is raised. But the e.Row.Item is the object before editing, so you don't see the new values. I understand that because of the EditEnding. In Silverlight you have an EditEnded event, how can I get the object with the new values when I edit the DataGrid.

thanks,

开发者_运维技巧

Filip


From https://social.msdn.microsoft.com/Forums/en-US/c38fc695-d1ec-4252-87b7-feb484ee01e4/wpf-4-datagrid-roweditending, change the UpdateSourceTrigger of the Binding to PropertyChanged. The Property will then be updated immediately, before the RowEditEnding event, and the new value can be accessed from the RowEditEnding event handler.

For example, for a DataGridComboBoxColumn

SelectedItemBinding="{Binding ForTestResult, UpdateSourceTrigger=PropertyChanged}"

This seems a very simple way to solve this issue.

In addition, although I have not tried it, I think it should be easy to also access the original value before editing if your object implements IEditableObject.


Well, maybe this may help: http://wpf.codeplex.com/Thread/View.aspx?ThreadId=39356

http://blogs.msdn.com/b/vinsibal/archive/2009/04/14/5-more-random-gotchas-with-the-wpf-datagrid.aspx

Or this, see point number 5.

You'll have to tinker with it to get what you want I think, but I hope that helps! Or points you in a good direction.


This solution seems simple enough. Referred from msdn forum.

private void dgEmployees_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{           
    Action action = delegate
                  {
                     Employee emp = e.Row.Item as Employee;
                    //do something nice to the employee                
                   };
    Dispatcher.BeginInvoke(action, System.Windows.Threading.DispatcherPriority.Background);
}


Attach to the ObservableCollection's changed event.

I bound to a DataTable and used the RowChanged event.


My fresh and IMHO fastest way is to add bool rowEdited=false, then set it to true inside DataGrid_RowEditEnding and put your code for 'editEnded' inside DataGrid_LayoutUpdated:

if (rowEdited)
{
    //main code here//
    rowEdited=false;
}

.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号