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

Programmatic scrolling from ViewModel

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 



1 Reply

DB Dinesh Babu Yadav Syncfusion Team July 24, 2017 11:00 AM UTC

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 


Loader.
Up arrow icon