We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to pass selected itens to viewmodel?

Hello guys,

I'm doing this the wrong way in the code behind:

private void ListView_OnSelectionChanged(object sender, ItemSelectionChangedEventArgs e)
        {
            viewModel.SelectedItens = listView.SelectedItems;
        }

How can I bind this?

3 Replies

MK Muthu Kumaran Gnanavinayagam Syncfusion Team May 5, 2017 07:28 AM UTC

Hi André Lacerda Murta Chaves, 
 
We have analyzed the reported query “Access the selected items of SfListView in ViewModel” and you can achieve your requirement by using EventToCommand behaviors. Here, we have created a Command for SelectionChanged event of SfListView in ViewModel and bind the event to SfListView using Behaviors. We have already created a KB regarding this and you refer the below link for more information. 
 
 
Code Example[C#]: 
public class ContactsViewModel 
{ 
  Command< SfListView > selectionChangedCommand;  
        
  public Command< SfListView > SelectionChangedCommand 
  { 
    get { return selectionChangedCommand; } 
    protected set { selectionChangedCommand = value; } 
  } 
   
  public ContactsViewModel() 
  { 
    selectionChangedCommand = new Command< SfListView >(OnSelectionChanged); 
  } 
 
  private void OnSelectionChanged(SfListView listView) 
  { 
    var selectedItems= listView.SelectedItems; 
  } 
} 
 
Code Example[XAML]:  
<listView:SfListView ItemSize="70" ItemsSource="{Binding contactsinfo}"> 
  <listView:SfListView.Behaviors> 
   <local:EventToCommandBehavior EventName="SelectionChanged" Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding Source={x:Reference listView}}"/> 
  </listView:SfListView.Behaviors> 
</listView:SfListView> 
 
For your assistance we have attached the working sample below. 
 
 
Please let us know if you require further assistance. 
 
Regards, 
G.Muthu Kumaran. 



AL André Lacerda Murta Chaves May 5, 2017 02:47 PM UTC

Thanks a lot!

That solved my problem.


DB Dinesh Babu Yadav Syncfusion Team May 8, 2017 04:43 AM UTC

Hi Andre, 
 
Thanks for the update. 
 
Regards, 
Dinesh Babu Yadav 


Loader.
Up arrow icon