What is the proper way to handle multiple datagrids in a tab control so that cells leave edit mode when the tabs are changed?

后端 未结 4 1088
旧时难觅i
旧时难觅i 2020-12-03 07:23

In wpf I setup a tab control that binds to a collection of objects each object has a data template with a data grid presenting the data. If I select a particular cell and p

4条回答
  •  [愿得一人]
    2020-12-03 08:09

    What I think you should do is pretty close to what @myermian said. There is an event called CellEditEnding end this event would allow you to intercept and make the decision to drop the unwanted row.

    private void dataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            DataGrid grid = (DataGrid)sender;
            TextBox cell = (TextBox)e.EditingElement;
            if(String.IsNullOrEmpty(cell.Text) && e.EditAction == DataGridEditAction.Commit)
            {
                grid.CancelEdit(DataGridEditingUnit.Row);
                e.Cancel = true;
            }            
        }
    

提交回复
热议问题