|
-
Dec 17th, 2009, 07:40 PM
#1
Thread Starter
Hyperactive Member
Updating Class Variable
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:
csharp 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 ?
-
Dec 22nd, 2009, 11:16 AM
#2
Hyperactive Member
Re: Updating Class Variable
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.
-
Dec 22nd, 2009, 11:24 AM
#3
Thread Starter
Hyperactive Member
Re: Updating Class Variable
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.
-
Dec 22nd, 2009, 12:05 PM
#4
Re: Updating Class Variable
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
-
Dec 22nd, 2009, 02:05 PM
#5
Thread Starter
Hyperactive Member
Re: Updating Class Variable
You are right, but what would you suggest I should do to update the Price every time one Transaction in the collection changes ?
-
Dec 22nd, 2009, 02:23 PM
#6
Re: Updating Class Variable
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:
Code:
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)
-
Dec 22nd, 2009, 02:36 PM
#7
Thread Starter
Hyperactive Member
Re: Updating Class Variable
Thanks for your suggestion, I'll try it and let you know.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|