2011-05-12 172 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一起工作的麻煩。