簡體   English   中英

在WPF數據網格中,如何在單元格編輯后獲得新行?

[英]In a WPF datagrid, how do I get the new row after cell edit?

我的DataGrid綁定到一個DataTable 在網格中的每個單元格編輯之后,我需要獲取新的DataRow值。 只有在焦點從行而不是單元格丟失后,我才能獲得新值。

<DataGrid ItemsSource="{Binding}" CellEditEnding="grid_CellEditEnding"/>


private void grid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{  
   //Both the DataTable and the row obtained from "e" has the old row. 
}

我應該怎么做才能獲得新的DataRow

顧名思義, CellEditEnding事件在提交編輯之前立即被觸發。 可悲的是,由於沒有CellEditEnded事件,所以沒有簡單的方法可以執行所需的操作。

您在這里有三個選擇:

  1. 將單元的Bindings UpdateSourceTriggerPropertyChanged ,這樣更改將立即應用,而不必等待提交。
  2. 將單元的Bindings NotifyOnSourceUpdatedTrue ,然后在代碼隱藏中添加Binding.AddSourceUpdatedHandler(MyDataGrid,OnDataGridSourceUpdated) (有關詳細信息, 請參見此處: http : Binding.AddSourceUpdatedHandler(MyDataGrid,OnDataGridSourceUpdated) )。
  3. 或處理CellEditEndingRowEditEnding事件。 在CellEditEnding中,使用Dispatcher.BeginInvoke強制執行DataGrid.CommitEdit()調用。 然后在RowEditEnding中,再次使用BeginInvoke執行代碼。 它會在提交行編輯后執行,但是您必須通過其索引或類似方式手動檢索行值(在此處查看類似示例(第5點): http : //blogs.msdn.com /b/vinsibal/archive/2009/04/14/5-more-random-gotchas-with-the-wpf-datagrid.aspx)-請記住,由於您無法控制何時執行代碼,您的商品可能會進一步更改,重新排列或類似的東西,因此,如果沒有您的部分工作,這並不是100%可靠的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM