- Home
- Forum
- Xamarin.Forms
- How to pass selected itens to viewmodel?
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?
SIGN IN To post a reply.
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.
KB link: https://www.syncfusion.com/kb/7523/how-to-turn-events-into-commands-with-behaviors-in-sflistview
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
SIGN IN To post a reply.
- 3 Replies
- 3 Participants
-
AL André Lacerda Murta Chaves
- May 3, 2017 03:58 AM UTC
- May 8, 2017 04:43 AM UTC