Search code examples
c#indexoflonglistselector

Get Index of a LongListSelector SelectedItem with JumpList and DataSource as List<AlphaKeyGroup<Artist>> where Artist is a custom class


I have the below class ArtistAlbum:

    public class Artist
{
    public string ArtistName { get; set; }
}

This LonglistSelector belongs to the MusicPage.xaml and after getting the Index of the LongListSelector.SelectedItem, I would like to retrieve the details of the Artist Collection at the above retrieved Index at a different page and hence the use of GlobalVars. But, I'm not able to get the Index of the LongListSelector.

private void llsArtists_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Getting the index here from llsArtists.ItemsSource by using IndexOf
    // doesn't work and returns -1
    // Get the ID here and save it to GlobalVars._artistID;
    NavigationService.Navigate(new Uri("/ArtistDetail.xaml", UriKind.Relative));
}

The below method is responsible for getting the results into the collection.

void GetArtists()
    {

        this.Dispatcher.BeginInvoke(() =>
        {

            for (int i = 0; i < ml.Artists.Count; i++)
            {
                sourceArtists.Add(new Artist
                {
                    ArtistName = ml.Artists[i].Name.ToUpper(),
                });


                List<AlphaKeyGroup<Artist>> DataSource = AlphaKeyGroup<Artist>.CreateGroups(sourceArtists,
                      System.Threading.Thread.CurrentThread.CurrentUICulture,
                      (Artist s) => { return s.ArtistName; }, true);

                    llsArtists.ItemsSource = DataSource;

            }


        });
    }

Please see the image below for the ItemSource structure Nested List


Solution

  • This has solved my query:

    As my ItemsSource was originally Artist but it was then converted to List<AlphaKeyGroup<Artist>>, I then selected my List<AlphaKeyGroup<Artist>> as the ItemsSource to get the .IndexOf by using the following:

    1. I first got the Model.Name attribute of the e.AddedItems by using this void

       private void llsArtists_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          // Getting the index here from llsArtists.ItemsSource by using IndexOf
          // doesn't work and returns -1
          // Get the ID here and save it to GlobalVars._artistID;
          var artist = (Artist)e.AddedItems[0];
          GetSelectedArtist(artist.ArtistName);
          //int selectedIndex = llsArtists.ItemsSource.IndexOf(llsArtists.SelectedItem as AlphaKeyGroup<Artist>);
          NavigationService.Navigate(new Uri("/ArtistDetail.xaml", UriKind.Relative));
      }
      
    2. and then using LINQ, I iterated to get my SelectedItemIndex

    But I believe it could also be retrieved by a simple .IndexOf replacing the ItemsSource as the AlphaKeyGroup source. (Haven't checked this yet)

    public Artist GetSelectedArtist(string aName)
            {
                var result = (sourceArtists.Single(u => u.ArtistName == aName));
                var index = sourceArtists.IndexOf(result);
                GlobalVars._artistID = index;
                return result;
            }