How can I call a method after a property changes in another class?

时间:2019-03-19 14:39:12

标签: c# wpf

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:

  1. BasicPart: Defines part properties that are independent of location (part number, volume, density, mass)
  2. DetailedPart: A subclass of BasicPart that also contains location-based information (for example, CG location)
  3. StdPartLibrary: Has only one property (a List object) and a few methods for things like saving/loading the basic part information to/from a file
  4. MassModel: Has only one property (a List object) and a few methods for things like calculating the assembly's CG location

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!

1 个答案:

答案 0 :(得分:2)

因此,这是基础知识,CellEditEnd事件在保存在数据网格中的内容之间触发,并且在用户尝试停止编辑单元格之后,可以取消该事件以强制用户继续编辑,因此在事件结束之前source from docs

不提交(保存)更改

因此,您需要使用另一个事件,例如:this event CurrentCellChanged,该事件将在更改(提交/保存)单元格后触发,但不会告诉您哪个单元格是更改后的Scottlogic.com提供了以下示例代码,以结合CurrentCellChangedCellEditEnd来获取正在编辑的单元格,并在触发事件后触发:

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中找到)

,然后在提交数据后运行您的方法。