WPF DataGrid - Event for New Rows?

前端 未结 7 1934
梦谈多话
梦谈多话 2020-12-06 17:13

I\'m using the WPF DataGrid (.Net 3.5 SP 1 version from the Toolkit)

What event can I subscribe to, to detect when a new row is added?

7条回答
  •  半阙折子戏
    2020-12-06 17:51

    Objects are persisted (inserted or updated) when the user leaves a row that he was editing. Moving to another cell in the same row updates the corresponding property through data binding, but doesn't signal the Model (or the Data Access Layer) yet. The only useful event is DataGrid.RowEditEnding. This is fired just before committing the modified row.

    XAML

    
    
    

    Code Behind

    private void MyDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) 
    {    // Only act on Commit
        if (e.EditAction == DataGridEditAction.Commit)
        {
             var newItem = e.Row.DataContext as MyDataboundType;
             if (newItem is NOT in my bound collection) ... handle insertion...
        } 
    }
    

    All credits for this solution go to Diederik Krolls (Original Post). My respect.

提交回复
热议问题