2011-05-12 171 views
1

我有一个WPF数据网格,它适用于我想要的,但每行都有两个可以编辑的单元格。是否可以在编辑行时将这两行放入编辑模式,然后在行编辑结束时触发更新/行失去焦点?目前,在每个单元格被编辑后,RowEditEnding触发,并且用户必须等待提交后重新绘制UI。我使用的代码是:WPF DataGrid全行编辑

private bool isManualEditCommit; 
private void dg_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) 
     { 
      if(e.EditAction!= DataGridEditAction.Commit) 
       return; 
      var newProd = dgLists.SelectedItem as IProduct; 
      if(newProd==null) 
       return; 
        worker = new BackgroundWorker(); 
        worker.DoWork += (s, dwe) => 
        { 
      ... commit update 
        }; 
        worker.RunWorkerCompleted += (s, rwe) => 
        { 
         ... refresh grid 
        }; 
        worker.RunWorkerAsync(); 
     } 
    /// <summary> 
    /// Commits edits when a cell edit ends. 
    /// </summary> 
    /// <param name="sender">The source of the event.</param> 
    /// <param name="e">The <see cref="System.Windows.Controls.DataGridCellEditEndingEventArgs"/> instance containing the event data.</param> 
    private void dg_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { 
     if (e.EditAction == DataGridEditAction.Commit) 
     { 
      if (!isManualEditCommit) 
      { 
       isManualEditCommit = true; 
       DataGrid grid = (DataGrid) sender; 
       grid.CommitEdit(DataGridEditingUnit.Row, true); 
       isManualEditCommit = false; 
      } 
     } 

回答

0

全行编辑是默认功能。更新每单元格编辑触发更新的唯一原因是您已经实现了dg_cellEditEnding方法。

+0

但并非所有单元格都进入编辑模式。每个单元格在接收焦点时显示它的CellEditingTemplate,但该行中的其他单元格具有标准的,不可编辑的控件。 – Echilon 2011-05-22 09:51:57

1

我退出使用DataGrid进行编辑。我使用ListView然后提供然后提供一个GridView作为ListView.View。在GridView中,您可以使用CellTemplates创建GridViewColumns。每个GridView行的最后一列是一个用于删除该行的按钮。我不支持浏览和编辑模式,而只是支持编辑模式。应用程序移动更流畅,我没有任何与DataGrid一起工作的麻烦。