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

ItemSelected event of Listview equal in SfListview?

I cant figure out how to convert code below for XF listview into

listview.ItemSelected += (sender, args) =>
{
if (Pages.ContainsKey(((MenuItem)args.SelectedItem).Title))
{
Detail = Pages[((MenuItem)args.SelectedItem).Title];
}
IsPresented = false;
};

I tried using SelectionChanged withAddedItemslike below but it didnt help. Can you please help me here

listview.SelectionChanged+= (sender, args) =>
{
if (Pages.ContainsKey(((MenuItem)args.AddedItems).Title))
{
Detail = Pages[((MenuItem)args.AddedItems).Title];
}
IsPresented = false;
};

3 Replies

DB Dinesh Babu Yadav Syncfusion Team July 17, 2017 06:42 AM UTC

Hi Emil, 
 
Thank you for using Syncfusion Products. 
 
We would like to let to know that the AddedItems argument(gets the collection of underlying data objects where the selection going to process) from the SelectionChanged event is IList<object> type. The first item in the collection will be the SelectedItem so, you need to modify the condition as like below code snippet. 
 
Code Example[C#]: 
listview.SelectionChanged+= (sender, args) => 
{ 
if (Pages.ContainsKey(((MenuItem)args.AddedItems[0]).Title)) 
{ 
Detail = Pages[((MenuItem)args.AddedItems[0]).Title]; 
} 
IsPresented = false; 
}; 
  
  
Please let us know if you require further assistance. 
 
Regards, 
Dinesh Babu Yadav


EM Emil February 9, 2018 03:30 PM UTC

Hi,

This solution is unfortunately not working for me because i need SelectedChanged to be fired when it is set in the code but you have restriction as in the documentation stated.
How can I achieve that when I set selectedItem, I can fire an event like xamarin forms listview fires ItemSelected.

  _listView.SelectedItem = Items[0];


NOTE

SelectionChanging and SelectionChanged events will be triggered only on UI interactions.



MK Muthu Kumaran Gnanavinayagam Syncfusion Team February 12, 2018 12:21 PM UTC

Hi Emil, 
 
We have checked the reported query “Need to raise the SelectionChanged event when set the SelectedItems programmatically” from our side. As we have mentioned in our documentation, the SelectionChanged or SelectionChanging events will not be raised when adding the model items into SelectedItems property programmatically and raised only during touch interactions. This was the actual behavior of SfListView. 
 
However you can overcome this behavior and achieve your requirement by raising the Internal RaiseSelectionChanged method in SelectionController of SfListView using Reflection.  
 
Note: Make sure that you have hooked the SelectionChanged event before adding items to SelectedItems property. 
 
When the collection of the SelectedItems property gets changed, its CollectionChanged event will be raised. You can get the value of the Item added from the NewItems of NotifyCollectionChangedEventArgs from this event. Then you can pass the added item as parameters to the reflected RaiseSelectionChanged method and invoke this method as like below code example. 
 
Code Example[C#]: 
public partial class MainPage : ContentPage 
{ 
  public MainPage() 
  { 
    InitializeComponent(); 
    listView.SelectedItems.CollectionChanged += SelectedItems_CollectionChanged; 
    listView.SelectionChanged += ListView_SelectionChanged; 
    listView.SelectedItems.Add(viewModel.contactsinfo[3]); 
  } 
 
  private void SelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
  { 
    var Item = (e.NewItems.SyncRoot as IList<object>).ElementAt(0) as Contacts; 
    var method = listView.SelectionController.GetType().GetRuntimeMethods().FirstOrDefault(x => x.Name == "RaiseSelectionChanged"); 
    var addedItems = new List<object>(); 
    addedItems.Add(Item); 
    var removedItems = new List<object>(); 
    method.Invoke(listView.SelectionController, new object[] { addedItems, removedItems }); 
  } 
 
  private void ListView_SelectionChanged(object sender, ItemSelectionChangedEventArgs e) 
  { 
    //Your action here 
  } 
} 
 
For your reference, we have attached the sample and you can download it from the below location. 
 
 
Please let us know if you require further assistance. 
 
Regards, 
G.Muthu kumaran. 


Loader.
Live Chat Icon For mobile
Up arrow icon