tags:

views:

2770

answers:

6

I've got a WPF app using the Model-View-ViewModel pattern.
In my ViewModel I've got a ListCollectionView to keep a list of items.
This ListCollectionView is bound to a ListBox in my View.

<ListBox Grid.Row="1" ItemsSource="{Binding Useragents}" SelectionMode="Multiple"/>

The ListBox has SelectionMode=Multiple, so you can select more items at one time. Now the ViewModel needs to know which items has been selected.

The problem is: in the View-Model-ViewModel pattern the ViewModel has no access to the View, so I can't just ask the ListBox which items has been selected. All I have is the ListCollectionView, but I can't find a way to find which items has been selected in there.

So how do I find which items has been selected in the ListBox? Or a trick to achieve this (maybe bind something to a Boolean 'IsSelected' in my items? But what? How?)

Maybe someone who is using this pattern, too, can help me here?

+3  A: 

You need to create a ViewModel that has the concept of IsSelected on it and is bound to the IsSelected property of the actual ListBoxItem that represents it in the View using the standard WPF bindings architecture.

Then in your code, which knows about your ViewModel, but not the fact that it's represented by any specific View, can just use that property to find out which items from the Model are actually selected irrespective of the designers choice for how its represented in the View.

Sounds good, but how?Can you give me some more hints on how to achieve this with a ListBox and a ListCollectionView?
Sam
The collection you're going to bind to the LB's ItemsSource will contain instances of the VM class. Then you create a DataTemplate for this VM class which binds ListBoxItem.IsSelected to your class' IsSelected property. When the LB is populating, it will use that template automatically.
Well, I still got no clue how to do this, especially since (as I stated in my question) multiple items can be selected.
Sam
+1  A: 

Look at this blogpost by Josh Smith The Initially Selected Item when Binding to a Grouped ICollectionView

Lars Hildebrandt
+1  A: 

The solution of Drew Marsh works very well, I recommend it. And I have another solution !

Model View ViewModel is a Passive View, you can also use a Presentation Model to access some datas of your presentation without being coupled with WPF (this pattern is used in the Stocktrader example of PRISM).

Nicolas Dorier
A: 

Here is another variant of the View-Model-ViewModel Pattern where the ViewModel has access to the view through an IView interface.

I encountered quite a lot scenarios where you can't use WPF binding and then you need a way in code to synchronize the state between the View and the ViewModel.

How this can be done is shown here:

WPF Application Framework (WAF)

jbe
+1  A: 

Drew Marsh's answer is fine if you have a small list, if you have a large list the performance hit for finding all your selected items could be nasty! My favorite solution is to create an attached property on your ListBox that then binds to an ObservableCollection which contains your selected items. Then with your attached property you subscribe to the items SelectionChanged event to add/remove items from your collection.

David Rogers
Sounds promising - you don't happen to have some code samples for this?
Sam