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
close icon

Raising Property changed on SelectedItem doesnt fire SelectionChanging or SelectionChanged

I would like to change selectedItem only 1 way from viewModel to View and I expect SelectionChanging or/and SelectionChanged events are fired. 

  <sfListView:SfListView x:Name="list"   ItemsSource="{Binding Items}" SelectionMode="Single"  SelectionGesture="Tap"
             ItemSpacing="2" AllowSwiping="False"   SelectionChanging="list_SelectionChanging"  SelectionBackgroundColor="Yellow"  Orientation="Horizontal"
                               SelectedItem="{Binding SelectedItem, Mode=OneWay}" >

When I raise    RaisePropertyChanged("SelectedItem"); in ViewModel, it works fine, I can see selectedItem is changed but I want to scroll to the selectedItem, Thats why I tried SelectionChanging or SelectionChanged events but not luck. they arent fired. they are fired only when I tap on an item. What is the reason for that? How can I achieve my purpose? I need to scroll to selecteditem but selecteditem should be set in the ViewModel

      private void list_SelectionChanging(object sender, Syncfusion.ListView.XForms.ItemSelectionChangingEventArgs e)
        {
            try
            {
                (list.LayoutManager as Syncfusion.ListView.XForms.LinearLayout).ScrollToRowIndex(vm.SelectedItem.DisplayOrder);
            }
            catch (Exception ex)
            {
        
            }
        }



1 Reply

DB Dinesh Babu Yadav Syncfusion Team June 5, 2017 08:54 AM UTC

Hi Emil, 
 
Sorry for the inconvenience caused. 
 
We would like to let you know that the SelectionChanging and SelectionChanged events are triggered only when the selection is performed by gesture action instead of programmatic selection. However, the reported requirement “Need to scroll to selected item in SfListView” can achieved by sample level. While initializing the SfListView, you can scroll to SelectedItem through Loaded event in which you can get the selected item’s index from the DisplayItems.Indexof() method and scroll to that item by using ScrollToRowIndex method as like below code snippet. 
 
Code Example[C#]: 
private void ListView_Loaded(object sender, Syncfusion.ListView.XForms.ListViewLoadedEventArgs e) 
{ 
 (listView.LayoutManager as LinearLayout).ScrollToRowIndex(listView.DataSource.DisplayItems.IndexOf(ViewModel.SelectedItem));   
} 
 
Also, if you are changing the SelectedItem (ViewModel property) through button click event or any gesture action at runtime, you can use the ScrollToRowIndex method in the clicked or gesture event as same like above code snippet. 
 
For your reference, we have attached the sample and you can download it from the below link. 
 
 
Please let us know if you require further assistance. 
 
Regards, 
Dinesh Babu Yadav 
 


Loader.
Live Chat Icon For mobile
Up arrow icon