Either I do not see the solution or I found a pitfall in using MVVM.
I have this sample Master-Detail:
class Customer
{
int CustomerID {get;set}
string Name {get;set}
ObservableCollection<Order> Orders {get;set}
}
class Order
{
int OrderID {get;set}
int Quantity {get;set}
double Discount {get;set}
}
Lets assume in my CustomerOrdersViewModel my ObservableCollection Customers is bound to the View via ...="{Binding Customers}" and when the customer is changed from the user the relating Orders are shown in the DataGrid via ItemsSource="{Binding SelectedItem.Orders, ElementName=comboboxCustomer}".
This is possible with MVVM:
I can add a new Customer by simply (for simplicity's sake) calling Customers.Add(new Customer(){...});
.
After the adding I do this: this.RaisePropertyChanged("Customers");
. This will update the view and immediately show the Customer in the Customer-Combobox.
Now comes the impossible part with MVVM.
I can add a new Order by SelectedCustomer.Orders.Add(New Order(){...});
BUT I cannot raise a CollectionChanged/PropertyChanged event like before with the Customers now on the Orders because the Orders Property is not bound to the View via public accessor.
Even if I would expose Orders bindable property to the view, the view itself cares for the Master-Detail switching not the ViewModel...
QUESTION: How is it possible to make Master-Detail work with Add/Del objects in Details-List and immediate update on the View ?