Translate

Friday, February 20, 2009

Silverlight 2 crashing with SelectedItem/SelectedIndex after repopulating ComboBox

I have a Silverlight form with a ComboBox that is populated with all 50 States. Whenever a state is chosen, a list of cities are retrieved and sent back to another ComboBox.

// Remove the old cities and populate with the new cities, selecting
// the proper city if it exists for the user profile.
Cities.Items.Clear();

ComboBox item = new ComboBoxItem();
item.Content = "Other";
Cities.Items.Add(item);
Cities.SelectedItem = item;

foreach(string city in retrievedCities)
{
item = new ComboBoxItem();
item.Content = city;
Cities.Items.Add(item);

if(city == db_profile_city)
Cities.SelectedItem = item;
}

This block of code above crashes when the SelectedItem of Cities is changed, not with an exception but Application_Exit is called. When the items are cleared from the ComboBox, the selected indicies don't seem to be updated, so when the SelectedItem is changed, the engine crashes with the 'white screen of death'.

The fix to this is to call Cities.UpdateLayout() after calling Cities.Items.Clear().

No comments:

Post a Comment

Codementor

Ryan Kuhn

★★★★★

Expertise