Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP8 LongListSelector - re-assigning ItemsSource has no effect

I'm using the new Windows Phone 8 LongListSelector control, which has its ItemsControl assigned to a List<Group<object>> as so:

    List<Group<PlacePoint>> searchResults; 

    async void doSearch()
    {
        this.searchResults = await SearchHelper.Instance.getSearchResults(txtSearchTerm.Text);
        longList.ItemsSource = this.searchResults;
    }

Unfortunately, the second time that I search, re-setting the .ItemsSource property has no effect and the control simply displays the old List.

How can I change the binding?

like image 877
Carlos P Avatar asked Nov 30 '12 18:11

Carlos P


1 Answers

It would seem that re-assigning longList.ItemsSource does not have any effect, whether this is a bug or by design I can't say.

However, an easy workaround is simply to use an ObservableCollection> instead and then work with this collection rather than re-assigning the ItemsSource.

Sample code:

    ObservableCollection<Group<PlacePoint>> searchResults = new ObservableCollection<Group<PlacePoint>>();


    public SearchPage()
    {
        InitializeComponent();

        longList.ItemsSource = this.searchResults;
    }

    async void doSearch()
    {
        List<Group<PlacePoint>> tempResults = await SearchHelper.Instance.getSearchResults(txtSearchTerm.Text);

        // Clear existing collection and re-add new results
        this.searchResults.Clear();
        foreach (Group<PlacePoint> grp in tempResults )
        {
            this.searchResults.Add(grp);
        }
    }
like image 137
Carlos P Avatar answered Sep 30 '22 04:09

Carlos P