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.
Unfortunately, activation email could not send to your email. Please try again.

Programmatic scrolling from ViewModel

Thread ID:

Created:

Updated:

Platform:

Replies:

131681 Jul 23,2017 09:58 AM Jul 24,2017 07:00 AM Xamarin.Forms 1
loading
Tags: SfListView
Emil
Asked On July 23, 2017 09:58 AM

Is there a way to scroll to an index / item from viewmodel ? I am using mvvm and having some action on selecteditem in viewmodel. once it is completed, i would like to scroll to next item within viewmodel 



Dinesh Babu Yadav [Syncfusion]
Replied On July 24, 2017 07:00 AM

Hi Emil, 
 
Thank you for contacting Syncfusion support. 
 
The reported requirement “Scroll to next selected item in ViewModel using MVVM pattern” can be achieved by using Command property and by passing the SfListView events through Behaviors to ViewModel. By using SelectionChanged event and passing the EventName as SelectionChanged in the custom behavior class(EventToCommandBehavior) as like below code snippet which passes the ItemSelectionChangedEventArgs as parameter of SelectionChangedCommand command in ViewModel class. Also, you can access the SfListView in ViewModel class through LoadedCommand as like below code snippet in which ListView parameter is passed as argument through Behaviors and CustomConvertor. So, whenever an item is selected, you can be get the SelectedItem and then you can scroll to next item as like below code snippet using ScrollToRowIndex method.  
 
Code Example[XAML]: 
<listView:SfListView x:Name="listView" ItemSize="70" ItemSpacing="0,0,5,0" 
                           ItemsSource="{Binding contactsinfo}"> 
   <listView:SfListView.Behaviors> 
     <local:EventToCommandBehavior EventName="Loaded" 
                                   Command="{Binding LoadedCommand}" 
                                   Converter="{StaticResource EventArgs}" /> 
     <local:EventToCommandBehavior EventName="SelectionChanged" 
                                   Command="{Binding SelectionChangedCommand}" 
                                   Converter="{StaticResource EventArgs}" /> 
   </listView:SfListView.Behaviors> 
</listView:SfListView> 
 
Code Example[C#]: 
public void OnListViewLoaded(SfListView listView) 
{ 
  ListView = listView; 
} 
 
public async void OnSelectionChanged(ItemSelectionChangedEventArgs eventArgs) 
{ 
  if (ListView.SelectedItem != null) 
     index = contactsinfo.IndexOf(ListView.SelectedItem as Contacts); 
  await Task.Delay(500); 
  (ListView.LayoutManager as LinearLayout).ScrollToRowIndex(index + 1); 
} 
 
For your reference, we have attached the sample and you can download it form the below link. 
 
 
Please let us know if you require further assistance. 
 
Regards, 
Dinesh Babu Yadav 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;