Get the index of the selected item in longlistselector

前端 未结 2 1406
死守一世寂寞
死守一世寂寞 2021-01-16 03:24

I have a long list selector


 

        
相关标签:
2条回答
  • 2021-01-16 03:59

    sender is going to be who sent the fact that this event occurred. See SelectionChangedEventArgs at MSDN to know that you'll want to do e.AddedItems[0] if single-select list, or if multi-select list, you'll need to loop over it:

    foreach(var item in e.AddedItems)
    
    0 讨论(0)
  • 2021-01-16 04:10

    When the SelectionChanged event occurs, the senderparameter of the event handler represents the object that triggered this event. It is of type Object, but you can cast it to match your specific control type.

    In this case, the LongListSelector :

    var myItem = ((LongListSelector) sender).SelectedItem as Model;
    

    (Model represents the type of data your control handles).

    Afterwards, look for that item in the ItemsSource and retrieve its value :

    var myIndex = ((LongListSelector) sender).ItemsSource.IndexOf(myItem);
    

    You have named your control, so instead of (sender as LongListSelector), you could use its name, BTDevices, but the code lines I wrote was intended to show you what's what with the sender object.

    Alternatively (and this is a more elegant way), shown by bland, you could use the EventArgs for selection : e.AddedItems[0]

    0 讨论(0)
提交回复
热议问题