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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

ItemSelected event of Listview equal in SfListview?

Thread ID:

Created:

Updated:

Platform:

Replies:

131550 Jul 16,2017 04:16 PM UTC Feb 12,2018 12:21 PM UTC Xamarin.Forms 3
loading
Tags: SfListView
Emil
Asked On July 16, 2017 04:16 PM UTC

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;
};

Dinesh Babu Yadav [Syncfusion]
Replied On 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

Emil
Replied On 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.


Muthu Kumaran Gnanavinayagam [Syncfusion]
Replied On 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. 


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.

Warning Icon 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.Close Icon

;