Windows Phone Listbox: ensure ItemSource is bound before SelectedItem

Just spent a couple of hours trying to figure out why setting the SelectedItem of a ListBox kept crashing the application. It turns out that setting the ItemSource binding before the SelectedItem is a no go as per this StackOverflow posting.

So beware when designing your Windows Phone pages. For example, look at the snippet below.

 <toolkit:ListPicker            
            Header="Select a stop"
            Margin="{StaticResource PhoneMargin}"
            Grid.Row="2" 
            Grid.Column="0"
            SelectionMode="Single"
             ItemsSource="{Binding Stops}"
            SelectedItem="{Binding SelectedStop, Mode=TwoWay}">

If you place SelectedItem before ItemSource application will crash when you attempt to programmatically set the SelectedItem.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s