views:

216

answers:

2

I'm sure I am missing some of the basics here so please bear with me.

I have a two objects, a SalesOrder and a SalesOrderLineItem. The lineitems are kept in an Observablecollection<SalesOrderLineItem> of SalesOrder, and if I add line items my databound listView knows about the new items.

Now where I am having a problem is here: The SalesOrder has a read only property called "OrderTotal", which knows what the total price from all the line items combined are.

If I change the quantity, or price of a line item, I don't seem to know how to get change notification to bubble up to "OrderTotal".

Both classes inherit from, and fire INotifyPropertyChanged.

Wham am I missing?

+2  A: 

The ObservableCollection only notifies when an Item is added, removed, or when the list is refreshed, not when a property of an Item within the collection changes.

The SalesOrder class needs to be listening to each SalesOrderLineItem's PropertyChanged event.

-

The OrderTotal property could also be dynamic, generating the total each time...

Aaron Hoffman
This is what I am thinking, but WPF seems to have so much power, that I am wondering if there is a different solution. I'll go with this if not, but I feel like there should be a more elegant way to deal with this.
Russ
Unfortunately I agree with Aaron, you'll have to listen to each SalesOrderLineItem, and to the collection. WPF does have RoutedEvents that bubble, but these only apply in the visual tree; you can't use them in some arbitrary object graph.
Robert Macnee
I agree. Binding in WPF is very powerful, and there may be a better way to do this. You may want to ask your question in a different way. Right now it leads me to believe you are looking for a way the ObservableCollection will solve this problem. Ask it again more generically-and with diff subject
Aaron Hoffman
Yea, I'm sure I am asking a number of questions wrong. I think for now I will go with this method, and just resolve myself to not knowing what I don't know for now. 3 Months from now I'm sure I will have a much better idea of how to ask.
Russ
A: 

If you feel adventurous, you can try BindableLinq or something similar. It allows you to expose the result of a query that listens to changes.

But it's not out of beta yet, and I'm not sure it will ever be... ContinuousLinq is another one, but it does not support Silverlight at all yet.

Other than that, you'll have to listen to each child, and add/remove handlers whenever the collection changes.

Denis Troller
I'm not ready to become dependant on even more assemblies.
Russ