I've been trying to figure this one out for a while and I feel like I'm close to a solution, but that I'm missing some crucial bit of information. I'm hoping someone here can help point me in the right direction.
I'm writing a program to track weight and center of gravity for an assembly. There are four main classes that I've got:
The user interface is a WPF form with, among other things, a datagrid showing the StdPartLibrary data and a table based on MassModel data showing the assembly's total mass and CG information. And I've implemented the INotifyPropertyChanged interface on the BasicPart class so the datagrid will automatically recalcualte mass, volume, and/or density when the user changes anything in the table.
But here's my problem:
I can't figure out a way to update the table to automatically recalculate the total mass and CG location when the user updates data in the datagrid. Right now, I have a button that the user has to press in order to update the total mass and CG location information and that does the job. But I'm perseverating on this now and really want to figure this out.
From what I've been reading, it sounds like I need to somehow subscribe to the PropertyChangedEventHandler in BasicPart but I can't seem to figure out exactly how or where I would implement that. Any advice?
ANSWER
So it turns out that I'm an idiot. As Steve Byrne pointed out in the comments, I just needed to run my code to update the mass properties in the CurrentCellChanged event of the DataGrid. Thanks!
答案 0 :(得分:2)
因此,这是基础知识,CellEditEnd
事件在保存在数据网格中的内容之间触发,并且在用户尝试停止编辑单元格之后,可以取消该事件以强制用户继续编辑,因此在事件结束之前source from docs
因此,您需要使用另一个事件,例如:this event CurrentCellChanged
,该事件将在更改(提交/保存)单元格后触发,但不会告诉您哪个单元格是更改后的Scottlogic.com提供了以下示例代码,以结合CurrentCellChanged
和CellEditEnd
来获取正在编辑的单元格,并在触发事件后触发:
private DataRowView rowBeingEdited = null;
private void dataGrid_CellEditEnding(object sender,
DataGridCellEditEndingEventArgs e)
{
DataRowView rowView = e.Row.Item as DataRowView;
rowBeingEdited = rowView;
}
private void dataGrid_CurrentCellChanged(object sender, EventArgs e)
{
if (rowBeingEdited != null)
{
rowBeingEdited.EndEdit();
}
}
(Code's source, from blog.scottlogic.com posted by Colin E.)
最后,还有另一个更复杂的解决方案,那就是通过调用
来强制将数据提交到单元格 grid.CommitEdit(DataGridEditingUnit.Row, true);
(完整的教程,其原始代码可以在here中找到)
,然后在提交数据后运行您的方法。