There are some posts discussing adding data-binding ability for ListView.SelectedItems
with non-trivial amount of code. In my scenario I don't need to set it from the ViewModel
, just getting selected items in order to perform action on them and it is triggered by command so push update is also not necessary.
Is there a simple solution (in terms of lines of code), maybe in code-behind? I am fine with code-behind as long as View
and ViewModel
don't need to reference each other. I think this is a more generic question: "best practice for VM to get data from the View on-demand", but I can't seem to find anything...
To get the
SelectedItems
only when a command is executed then useCommandParameter
and pass in theListView.SelectedItems
.I can assure you: SelectedItems is indeed bindable as a XAML CommandParameter
After a lot of digging and googling, I have finally found a simple solution to this common issue.
To make it work you must follow ALL the following rules:
Following Ed Ball's suggestion', on you XAML command databinding, define CommandParameter property BEFORE Command property. This a very time-consuming bug.
Make sure your ICommand's CanExecute and Execute methods have a parameter of object type. This way you can prevent silenced cast exceptions that occurs whenever databinding CommandParameter type does not match your command method's parameter type.
For example, you can either send a listview/listbox's SelectedItems property to you ICommand methods or the listview/listbox it self. Great, isn't it?
Hope it prevents someone spending the huge amount of time I did to figure out how to receive SelectedItems as CanExecute parameter.
Since none of the other answers helped me (using
SelectedItems
asCommandParameter
was alwaysnull
), here is a solution for Universal Windows Platform (UWP) apps. It works usingMicrosoft.Xaml.Interactivity
andMicrosoft.Xaml.Interactions.Core
.Here's the View:
Here's the ViewModel (
RelayCommand
is a class from MVVM Light):Beware that if you are going to remove items from the original collection after the selection is completed (user pushes a button etc.), it will also remove the items from your
_selectedItems
list! If you do this in a foreach loop, you'll get anInvalidOperationException
. To avoid this, simply add a guard in the marked place like:and then in the method where you for example remove the items, do this:
I don't think it's correct condition to consider that 'View and ViewModel don't need to know each other'; In MVVM view always know about ViewModel.
I have also come across this kind of situation where I had to access ViewModel in view's code behind and then populate some data(like selected items), this becomes necessary while using 3'rd party controls like ListView, DataGrid etc.
If directly binding the VM property is not possible then I would listen to ListViw.SelectionChanged event and then update my ViewModels SelectedItems property in that event.
Update:
To enable VM pull data from view, You can expose an interface on the View that handles View-specific functionality and ViewModel will have reference of your View through that interface; Using an interface still keeps the View and ViewModel largely decoupled but I genrally don't prefer this.
MVVM, providing the Association of View to ViewModel
I would still prefer the approch of handling the event in View and keep the VM updated(with the selected items), this way VM don't need to worry about pulling the data before performing any operation, it just needs to use the data available(as that will always be updated one).
This can be achieved using Interaction triggers as below
You will need to add reference to
Microsoft.Expression.Interactions System.Windows.Interactivity
Add below xmlns to your xaml
Add code below just inside your GridView tag
Code Inside ViewModel declare property below
within constructor of Viewmodel initialize Command as below