Yossi Dahan [BizTalk]


Friday, January 21, 2011

WP7, PivotControl and data binding

I’m playing with windows phone 7 a little bit (anybody isn’t?) and  bumped into this today -

I have a page with a PivotControl, and the PivotControl is bound to an ObservableCollection.
When the page is loaded it calls a service asynchronously and populates the collection with results from the service.

On first run everything seemed ok, but playing around with the application it would often crash if I refresh the data on the page.

It took me a little bit of time to figure out the pattern, but this seemed to happen if I navigated a bit around the control before refreshing the data, and then I found this useful thread.

Basically – there’s a bug in the control which is a bit unhappy with the collection being changed after a selection was made and the easiest way to get around this (well – the only one that I got to work) is what’s described in that thread – remove the control’s binding before updating the list, and re-apply them afterwards. a bit clunky, but works.

Also – as my refresh happens on the callback of an asynchronous service all I had to use the dispatcher to run the code to remove and re-add the binding on the UI thread -

            Dispatcher.BeginInvoke(() =>
                ResultsPivot.DataContext = null;

<update list here>

            Dispatcher.BeginInvoke(() =>
                //re-apply control binding
                ResultsPivot.DataContext = App.WeatherResultsViewModel;
                //hide progress bar, as we're now complete
                progress.Visibility = System.Windows.Visibility.Collapsed;



Post a comment

<< Home