7

I have a class (let's call it MyContainerClass)that is a container for several other classes (let's call them ClassA to ClassF). ClassA to ClassF inherit the same base class (let's call it MyBaseClass). In MyBaseClass I have an int property with the name Count, which ClassA to ClassF inherit. I also have an int property with the same name in MyContainerClass which is the sum of ClassA to ClassF.

Then I have a datagrid, and each row has one object of MyContainerClass. The columns of the grid show ClassA to ClassF. The last column in the grid shows a total (that is it is bound the Count property of MyContainerClass). Now I want that the total in the last column updates as soon as I change the Count property in any class in the A-F range. I implemented INotifyPropertyChanged in MyBaseClass and it does fire when I change the Count property in ClassA to ClassF. But the last column in my datagrid remains the same. Any clues?

EDIT: I have created a class diagram. enter image description here

The getCount() method returns objA.Count+objB.Count+...+objF.Count and is returned by the Count property in the MyContainerClass. The question is: if I update objA.Count I want the GUI (that is the column in my datagrid) to display the updated sum.

Peter
  • 135
  • 1
  • 7
  • You say that the datagird remains the same, but does the property itself change? In other words - Did you check to see if the `Count` property in the `MyContainerClass` is updated or not? – Blachshma Nov 14 '12 at 09:59
  • 1
    If you could post some XAML and some sourcecode for your classes then it will be easier to diagnose what is going wrong! – Stephen Holt Nov 14 '12 at 10:02
  • @Blachshma, it is updated. If I check the objects in the debugger I always see the correct numbers. Only the GUI does not update. – Peter Nov 14 '12 at 11:41
  • @StephenHolt, I'd have to change a lot of code to create a generic example. But I have created a class diagram. I hope that helps. – Peter Nov 14 '12 at 11:42

2 Answers2

4

Just implementing INotifyPropertyChanged is not enough for an automatic update. The MyContainerClass object needs to "subscribe" to the change notifications of the MyBaseClass objects in the other columns. One way you can handle this is by adding a method like the following to the MyContainerClass:

void SubscribeToCount(MyBaseClass item)
{
    item.PropertyChanged += (s,e) => 
        this.Sum += (e.NewValue - e.OldValue); 
}

And you call this method for all the objects in the other columns. (It's not clear from the question but I assume there is a wrapper class that includes instances of MyClassA-F and MyContainerClass, which you bind the datagrid to a collection of this wrapper class).

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • This solved the same problem I was having, where I needed an (initially, calculated) property to notify of changes to a different class. Thanks! – Dan Sep 22 '16 at 10:33
1

Okay, here is what I did after staring at Eren's solution for a while: I also implemented the INotifyPropertyChanged for the container class and registered the PropertyChanged event for each object.

objA.PropertyChanged += updateCount;
objB.PropertyChanged += updateCount;
objC.PropertyChanged += updateCount;

...and so on. Then I added the following method

private void updateCount(object sender, PropertyChangedEventArgs e)
{
    OnPropertyChanged("Count");
}

Now I have the behavior that I want.

Peter
  • 135
  • 1
  • 7
  • Great, that's pretty much the kind of solution I would have suggested having now understood your problem! I think you can mark your own answer as the accepted answer can you? – Stephen Holt Nov 14 '12 at 17:02
  • 1
    And instead of _updateCount_ you mean _updateSum_ and instead of `OnPropertyChanged("Count")` you mean `OnPropertyChanged("Sum")`, because a _Sum_ property in `MyContainerClass` just is the better name. – VisorZ May 23 '18 at 18:19