Firing PropertyChanged event in a complex, nested type in WPF

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-04-23T11:05:18Z Indexed on 2010/04/23 11:13 UTC
Read the original article Hit count: 188

Hey

I have a question about the PropertyChanged vent firing in WPF whne it is used in a complex type.

I have a class called DataStore and a list of Departments (an ObservableCollection), and each department again has a list of Products. Properties in the Product class that are changed also affect properties in the Department and DataStore class. How does each Product notify the Department it belongs to, and the DataStore (which is the mother class of all) that one or more of its properties have changed their values? Example: a product has a property NumberSoldToday and is bound. The Department has a property called TotalNumberOfProductsSold:

public int TotalNumberOfProductsSold
{
  get
  {
    int result = 0;
    foreach(Product p in this.products)
      result += p.NumberSoldToday;
    return result;
  }
}

And the data store has a property TotalProductsSold (for all departments):

public int TotalProductsSold
{
  get
  {
    int result = 0;
    foreach(Product p in this.deparments)
      result += p.TotalNumberOfProductsSold;
    return result;
  }
}

If all these properties are bound, and the innermost property changes, it must somehow notify that the value of the other 2 changed as well. How?

The only way I can see this happening is to hook up the PropertyChanged event in each class. Th event must also fire when deleting, adding to the collection of products and deparments, respectively.

Is there a better, more clever way to do this?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about databinding