I am using WPF and MVVM.
In my app it would be nice to select from a main screen an item and then press a button to see details. A new window should be opened and (multiple) selections can be made in a listview where the IsSelected is bound to the items in a ObservebleCollection.
When the user pressed that button again (for the same selection on the main screen) another window is opened with the same details. However, I assume that if a different selection is made, the first window is synced and will show the same selections.
I would like to have the two windows possible with different selections. Is this possible and what should be done to make it work?
Update:
Example: let’s say I would have a car with 4 wheels (Collection). The listviews in both windows show the 4 wheels (one item is one wheel). When I select wheel 1 and 2 in the list view I think in the other window also wheel 1 and 2 will be selected. But I want to select maybe 3 and 4 in window 2 and wheel 1 and 2 in window 1). And I don’t want to add another collection of 4 wheels otherwise I would have a car with 8 wheels … or 2 cars with 4 wheels but if I change wheel 3 and 4, it would change only the second car.
Update 2:
Btw, in my app:
- I have 0 to n windows with a list view.
- I create a window including a view model.
- Each view model is referencing the ‘real’ model.
The binding variable for the selected items are inside the real model, I guess I need to move this to the view model, but I don’t know how. Because the properties (of the wheels in the example) are in the ‘real’ model and those are binded also in the same list view (items).
I c
If there are only two lists, you could bind IsSelected to two separate properties (ie. IsSelectedInList1, IsSelectedInList2).
But really, a viewmodel is supposed to be an in-code view-less representation of your UI. So, if you have two (or N) copies of the list in your UI which are supposed to have different states, it makes sense to create two (or N) copies of that viewmodel in memory to represent it.
I think this is more true when thinking about your model. In your model, you want one instance of a particular car, and that car should have one collection of four wheels. But if you want two different views (with different states), it is OK to create two viewmodels from that model.