Thursday, December 23, 2010

Adding an item to a list and scrolling it into view #wp7dev

For reasons can't explain rightly explain, it was a bit tricky to add an item to a list (in a Windows Phone 7 application - unsurprisingly) and then scroll the list so the new item was displayed.

I eventually got this working by forcing the call to scroll the new item into view on the UI thread.

Consider this in a default DataBound application:

  private void ApplicationBarIconButton_Click(object sender, EventArgs e)
  {
    App.ViewModel.Items.Add(new ItemViewModel
                                {
                                  LineOne = "new L1",
                                  LineTwo = "new L2",
                                  LineThree = "new L3"
                                });
 
    Dispatcher.BeginInvoke(() =>
      MainListBox.ScrollIntoView(MainListBox.Items.Last()));
  }


Without doing this, the list will scroll to the item which was previously the last on the list.

0 comments:

Post a Comment