I’m using MVVM. I have two listview. The first listview is fine, i’m able to populate it, it’s an IEnuerable. What I want to achieve is when the items (row) in the first listview is clicked, I would like to add the selected to the second listview. Can anyone advise on this.
Share
To demonstrate how to do what you want using MVVM I assume that you have a
ItemViewModelrepresenting the items in the list views. You need to set theListView.ItemTemplateto render each item properly (or override theToStringmethod to return a string representation of the item).You need a
MainViewModelwith three properties:Items1containing the list of items. In this example the list is not updated soIEnumerable<ItemViewModel>is sufficient.SelectedItem1which will reference the currently selected item in the first list view (if any).Items2containing the list of items you have selected so far. Because this list will be updatedObservableCollection<ItemViewModel>is used.The only interesting code in
MainViewModelis the setter forSelectedItem1. This will be modified whenever the selection in the first list view changes. When this happens the selected item is added to theItems2collection.Note that this simple view-model doesn’t implement
INotifyPropertyChangedbecause it is not required for this example.The view is bound to the view-model with XAML like this (e.g. the
DataContextof whatever contains this XAML should be set to an instance ofMainViewModel):The first
ListViewis bound toItems1in the view-model. When the selection in theListViewis changed data-binding will ensure thatSelectedItem1is set in the view-model. The code in the setter will then update theItems2property and because this is anObservableCollection<T>new items added will be pushed using data-binding – in this case to the secondListView.In your case it is possible to handle a selection event in the
ListViewby binding theSelectedItemproperty. However, sometimes it is impossible to use data-binding to “handle events”. A solution may be to add an event handler in the code-behind of the view but that will often lead to unwanted dependencies between your view-model and the view. Instead you can use a Blend Behavior. By writing your ownBehaviorclass you can handle the event and convert into something that you can data-bind to thereby breaking the unwanted dependency. However, to solve your particular problem it is not required.Note that if you want to use a
Behavioryou no longer need the Blend SDK. You can use NuGet to add a dependency toBlend.Interactivity.Wpf(or a similar package depending on your framework) to get the single DLL required to use Blend Behaviors.So to expand on how to deselect items from the second list when they are clicked you need to use a behavior. Trying to use the same trick as above where an action is performed in the setter of a property bound to
SelectedItemof the secondListViewwill fail because adding a new item to the secondListViewmay immediately select this item which then promptly will remove the newly added item – that is not what you want.Here is a
MouseLeftButtonUpBehaviorthat without code-behind will allow you to execute a command when the left mouse button is released on a control:The XAML has to be modified to this (you can use NuGet to add a reference to the
Blend.Interactivity.Wpfpackage to be able to add interactions to controls):Two new properties is required in the view-model:
The
SelectedItem2is used to track which item is selected in the second list view. TheDeselectCommandis executed when a left mouse button up event is fired in the second list view. To actually do something useful you need to create a command. You can use aDelegateCommand. This class is not part of WPF but if you google it you can easily find a suitable implementation. ADelegateCommandis simply a way to create a WPFICommandthat executes a delegate of your choice.In the constructor of
MainViewModel:And then you need to implement
DeselectinMainViewModel:Putting all this together will remove items from the second list view when they are clicked and this without any code-behind in your view that otherwise could create unwanted dependencies from you view to your view-model (e.g. the code in the view would have to know that it should call
Deselecton the view-model).