PDA

Click to See Complete Forum and Search --> : Updating Class Variable


Pac_741
Dec 17th, 2009, 06:40 PM
Hello everyone,

I have a class holding information, which implements INotifyPropertyChanged. Everything works fine so far, everything gets updated when a change is made.

But my problem starts with this piece of code:



//List of TransActionProduct -- This is the class I mentioned
ObservableCollection<Transactions.TransactionProduct> _PackageProducts = new ObservableCollection<SuperMarket.Data.Transactions.TransactionProduct>();

//This property adds up all the prices in the _PackageProducts List and returns it
decimal _price;
public decimal Price
{
get
{
for (int i = 0; i < PackageProducts.Count; i++)
{
_price = _price + PackageProducts[i].TransactionProductPrice;
}
return _price;

}
set
{

_price = value;

//This class also implements INotifyPropertyChanged
NotifiyChange("Price");
}

}


The problem is that while changing the prices of the TransactionProduct the Price property from the class above doesn't get updated.

Does anyone know how can I make that property update while the TransactionProduct's price changes ?

r0k3t
Dec 22nd, 2009, 10:16 AM
Where does the TransactionProduct get updated? In the UI?
Why not use a datatrigger - I can't really see a the whole picture from what you posted but that seems like it might work.

Pac_741
Dec 22nd, 2009, 10:24 AM
Yes, TransactionProduct gets updated in the UI.
As I said, TransactionProduct is working great, the problem is my other class, which is a collection of TransactionProducts and a property "Price" which holds the total price of the TransactionProducts in the collection.

Thanks for your response.

techgnome
Dec 22nd, 2009, 11:05 AM
I'm not sure why there's a set on the Price property for a collection... especially when it doesn't then also update the list... to me, that's the kind of thing that should be a read only property.

-tg

Pac_741
Dec 22nd, 2009, 01:05 PM
You are right, but what would you suggest I should do to update the Price every time one Transaction in the collection changes ?

chris128
Dec 22nd, 2009, 01:23 PM
How are you determining whether or not the Price has been updated? My guess is that you are displaying the Price in a textblock or something on the window that is loaded once and is then not refreshed after the transactions have been changed. If you update one of the transactions and then just do a MessageBox.Show to display the price property, does that show the updated value? I'm guessing it will do.

Its all very well implementing INotifyChanged but when you just update an item in your observable collection, your Price property is not actually being set so this line will never get reached:
NotifiyChange("Price");
So the change notification will never occur.

Having said that, I'm not sure what the best way to resolve this would be... It depends how your program is structured really because the easiest way that I can see is to just have a public method in your class that raises the change notification and just call that each time you update a transaction, but that might not really be possible in your program (and there might be a better way)

Pac_741
Dec 22nd, 2009, 01:36 PM
Thanks for your suggestion, I'll try it and let you know.