new to WPF and MVVM, and trying to get my head around the normal way to do things.
Say I have an observable collection of customer objects. I can bind a ListView, or ItemsControl to this and they all display fine.
Now say I have some supplementary information about each item that is not stored in the customer object. Say perhaps a special offer is on, and certain customers are eligible for the offer, but this flag is not held in the customer object.
What is the standard way to display this supplementary info in the list?
My immediate thought is to create a new collection of some structure in my viewmodel that contains both a customer object, and this flag. I can then bind the list straight to this collection, and bind the various display components to the appropriate struct members.
However, this seems a bit of a waste when I already have an ObservableCollection of customers. If I want the new list to be “live”, then I need the new collection to subscribe and react to add/removes in the underlying customer collection.
Is this the way to do it? Or is there some way I can maintain the offer flags in some way separately, and yet still bind to them in my ListView?
Many thanks
When using the MVVM pattern, the ViewModel is the “model of your view”, i.e. it is a model that closely resembles your current view.
I can see why you might want you underlying model to store offers and customers separately.
Pseudocode:
However, you cannot bind a ListView to your customers and somehow correlate them with your offers. It is the role of your ViewModel to shape the Model so that it is easier to bind it to your view:
Your view model would create an
ObservableCollectionof theseCustomerViewModelinstances, using the collection of modelCustomerinstances, locating anOfferif one exists.This
ObservableCollection<CustomerViewModel>can be readily bound to your view.