Getting WPF ListView.SelectedItems in ViewModel
Solution 1
To get the SelectedItems
only when a command is executed then use CommandParameter
and pass in the ListView.SelectedItems
.
<ListBox x:Name="listbox" ItemsSource="{Binding StringList}" SelectionMode="Multiple"/>
<Button Command="{Binding GetListItemsCommand}" CommandParameter="{Binding SelectedItems, ElementName=listbox}" Content="GetSelectedListBoxItems"/>
Solution 2
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
xmlns:i="http://schemas.microsoft.com/expression//2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
Add code below just inside your GridView tag
<GridView x:Name="GridName">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding Datacontext.SelectionChangedCommand, ElementName=YourUserControlName}" CommandParameter="{Binding SelectedItems, ElementName=GridName}" />
</i:EventTrigger>
</i:Interaction.Triggers>
Code Inside ViewModel declare property below
public DelegateCommand<object> SelectionChangedCommand {get;set;}
within constructor of Viewmodel initialize Command as below
SelectionChangedCommand = new DelegateCommand<object> (items => {
var itemList = (items as ObservableCollection<object>).Cast<YourDto>().ToList();
}
Solution 3
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).
Solution 4
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.
private bool OnDeleteSelectedItemsCanExecute(object SelectedItems) { // Your goes heres } private bool OnDeleteSelectedItemsExecute(object SelectedItems) { // Your goes heres }
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.
NS.X.
Updated on April 06, 2020Comments
-
NS.X. about 4 years
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 theViewModel
, 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
andViewModel
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... -
NS.X. about 12 yearsSorry I wasn't clear. By not knowing each other I mean one does not reference the other. Also registering to
SelectionChanged
event is not exactly necessary as the ViewModel only needs to get selected items when a command is being executed. It's more like "How can VM pull data from the View on demand". -
NS.X. about 12 years
-
evanb about 12 years@user986080 I didn't realize that
SelectedItems
did not support binding. I removed that from the answer. However theCommandParameter
does work, I've tested it and was able to the list of selected items. -
evanb about 12 yearsMy XAML example shows a
ListBox
but I've also tested aListView
as well and was able to get the selected items from the command parameter. -
NS.X. about 12 yearsI think I am hitting this CommandParameter bug which is hassle to workaround. But ideally your answer is the right way to go.
-
WAQ over 10 yearsReally thanks a lot for providing this CommandParamter solution. Worked great for me
-
rhcpfan over 9 yearsWhat happens when
GetListItemsCommand
is a Command defined in the ViewModel? The first binding uses the ViewModel as the DataContext and the second binding to the parameter uses itself as a DataContext... -
EzaBlade over 6 yearsI don't remember where I saw this (someone else on StackOverflow) but CommadParameter must be defined before Command in your xaml for you to pass the SelectedItems as a parameter. It worked for me (Edit: oops sorry its also in one of the answers on this page)