Question

How to dynamically (in code behind) select few items in Windows Phone Toolkit's LongListMultiSelector. The property SelectedItems has no setter.

My code:

 private async void GetGenres()
    {
        var genres = await App.MusClient.GetGenresAsync();

        var preferencedGenresIdsList = App.GetFromIsoStorage<List<string>>(App.GENRES_IDS_KEY_STRING);

        GenresListBox.ItemsSource = genres;




        foreach (var item in GenresListBox.ItemsSource)
        {
            if (preferencedGenresIdsList.Contains(((Genre)item).Id))
            {
                LongListMultiSelectorItem container = GenresListBox.ContainerFromItem(item) as LongListMultiSelectorItem;
                if (container != null)
                {
                    container.IsSelected = true;
                }
            }
        }




        if (genres.Result == null || genres.Count == 0)
        {
            MessageBox.Show("No results available");
        }

    }
Was it helpful?

Solution

I found solution

private async void GetGenres()
    {
        var genres = await App.MusClient.GetGenresAsync();

        var preferencedGenresIdsList = App.GetFromIsoStorage<List<string>>(App.GENRES_IDS_KEY_STRING);

        GenresListBox.ItemsSource = genres;




        foreach (var item in GenresListBox.ItemsSource)
        {
            if (preferencedGenresIdsList.Contains(((Genre)item).Id))
            {
                GenresListBox.ScrollTo(item);
                LongListMultiSelectorItem container = GenresListBox.ContainerFromItem(item) as LongListMultiSelectorItem;
                if (container != null)
                {
                    container.IsSelected = true;
                }
            }
        }
        GenresListBox.ScrollTo(GenresListBox.ItemsSource[0]);



        if (genres.Result == null || genres.Count == 0)
        {
            MessageBox.Show("No results available");
        }

    }

Reason it didn't work is because item is not created on UI yet. GenresListBox.ScrollTo(item); - forces UI to create element!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top