1

如何在 Windows Phone Toolkit 的 LongListMultiSelector 中动态(在代码中)选择几个项目。属性 SelectedItems 没有设置器。

我的代码:

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

    }
4

1 回答 1

1

我找到了解决方案

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

    }

它不起作用的原因是尚未在 UI 上创建项目。GenresListBox.ScrollTo(item); - 强制 UI 创建元素!

于 2014-03-04T20:23:52.963 回答