2012-11-22 202 views
0

我正在努力與WPF中的DataGrid。我有一個ObservableCollection綁定到它。當用戶輸入第一個單元格時,其他單元格會相應更新。爲此,我訂閱了CellEditEnding事件以在第一個單元格更改後強制更新。WPF DataGrid - CellEditEnding事件更新數據

在這種情況下,我也更新MyClass的其他性質是這樣的:

private void DataGridTeilnehmer_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
    { 
     if (!commiting) 
     { 
      commiting = true; 
      DataGridTeilnehmer.CommitEdit(DataGridEditingUnit.Row, false); 
      commiting = false; 

      if (e.Column.DisplayIndex == 0) 
      { 
       MyClass data = (e.Column.GetCellContent(e.Row) as ContentPresenter).Content as MyClass; 
       data.pass = "nothing"; 
      } 
     } 

的問題是,網格不更新自身,「無」不被顯示,直到我進入編輯 - 綁定到屬性「傳遞」的單元格的模式,其中包含「無」。但我想立即展示它。

由於提前,
弗蘭克

PS:我跟很多(數據)網格在我的生活工作,但WPF電網是我遇到迄今最糟糕的。

+0

有你提到UpdateSourceTirgger,同時結合現場通行證。如果是,那麼你可能沒有在MyClass for Pass屬性中定義propertychanged事件。 –

+0

你可以顯示你綁定的dataGrid代碼嗎? –

+0

您是否在使用Mvvm處理DataGrid?如果是的話,那麼你可以更新DataGrid行的ViewModel,如果它是一個屬性改變的類,那麼所有的綁定都會被更新,同時也會更新Datagrid的行。 –

回答

1

正確的方法是如下,目前使用這種方式在我的軟件

private void MyWPFFrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
    { 
     if (e.Column.SortMemberPath.Equals("EndDate")) 
     { 
      if (((MyObject)e.Row.Item).EndDate.Equals(DateTime.MinValue)) 
      { 
       ((MyObject)e.Row.Item).Completed = 1; 
       ((MyObject)e.Row.Item).CompletedDescription = "YES"; 
      } 
      else 
      { 
       ((MyObject)e.Row.Item).Completed = 0; 
       ((MyObject)e.Row.Item).CompletedDescription = "NO"; 
      } 


      this.MyWPFFrid.CurrentItem = ((MyObject)e.Row.Item); 



      if (!e.Row.IsEditing) 
      { 
       this.MyWPFFrid.Items.Refresh(); 
      } 


     } 
    }