For the last three days I've been facing a quite a composite problem. I am trying to make an order taking app, to do this I wanted to have an ObservableCollection containing MainCategory items. Now the MainCategory items contained a CategoryName and another ObservableCollection of dishes. The dishes have those props: name, price and quantity. I did all this and the bindings and everything works perfectly.
The problem is at the bottom of the page (outside the ListView which is nested inside a Listview ) I want to have a TextBlock with the TotalValue that needs to change every time The quantity of a dish changes. I am using MVVM and I have done all the INotifyPropertyChanged to the props and the CollectionChanged, but this only works when I add or remove something. Not when I change the value of a prop in the second ObservableCollection.
Here is some code:
public class MenuCollection : ObservableCollection<MainCategories>,
INotifyCollectionChanged, INotifyPropertyChanged
{
public MenuCollection()
{
Add(new MainCategories("Category1", false));
this[0].SubMenuItems.Add(new Dishes("Dish1", 4));
}
}
public class MainCategories : MainViewModelBase
{
public ObservableCollection<Dishes> SubMenuItems{get; set;}
public MainCategories(string name, bool visible)
{
this.categoryName = name;
this.visible = visible;
SubMenuItems = new ObservableCollection<Dishes>();
}
public string categoryName;
public string CategoryName
{
get { return categoryName; }
set
{
if (categoryName != value)
{
categoryName = value;
OnPropertyChanged("CategoryName");
}
}
}
public bool visible;
public bool Visible
{
get { return visible; }
set
{
if (visible != value)
{
visible = value;
OnPropertyChanged("Visible");
}
}
}
}
public class Dishes : MainViewModelBase
{
public Dishes(string dishName, int price)
{
this.dishName = dishName;
this.dishPrice = price;
this.quantity = 0;
}
private string dishName;
public string DishName
{
get { return dishName; }
set
{
if (dishName != value)
{
dishName = value;
OnPropertyChanged("DishName");
}
}
}
private int dishPrice;
public int DishPrice
{
get { return dishPrice; }
set
{
if (dishPrice != value)
{
dishPrice = value;
OnPropertyChanged("DishPrice");
}
}
}
private int quantity;
public int Quantity
{
get { return quantity; }
set
{
if (quantity != value)
{
quantity = value;
OnPropertyChanged("Quantity");
}
}
}
}
I just want to find a way to keep my total value update when anything changes inside the Main observable collection, like the quantity of a product. is there anyway to fire the CollectionChanged event?


I would consider possibly not using an observable collection if you need updates when items in the collection change. Observable collection is only useful for noting when items are added or removed from a collection.
Something that I've done before is similar to the code below.
In your
MainCategoryyou would only expose anIEnumerable<Dish>instead of the full list. Then, inMainCategoryyou can create wrapping methods to add and remove dishes that subscribe to property changed notifications. Then every time a dish changes you can make updates to your aggregate values like Total and notify that your list of dishes has changed.