Steve
Steve

Reputation: 177

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

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!

Upvotes: 0

Views: 681

Answers (1)

Steve Byrne
Steve Byrne

Reputation: 1360

So here's the basics, the CellEditEnd event fires between the content being saved in the data grid and after the users tries to stop editing the cell, the event can be cancelled to force the users to continue editing and thus does not commit (save) changes until the event has ended source from docs

Because of this, you need to use another event, such as: this event CurrentCellChanged which will be fired after the cell is changed (committed/saved) but doesn't tell you which cell was changed Scottlogic.com supplies the example code below to combine CurrentCellChanged and CellEditEnd to both get the cell being edited, and fire after the event is fired:

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.)

Finally, there is another more complex solution, which would be to force a data commit to the cells by calling

  grid.CommitEdit(DataGridEditingUnit.Row, true);

(A full tutorial, and the original code can be found here)

and then running your method now that the data has been committed.

Upvotes: 2

Related Questions