Posts Tagged selection lost on ItemsSource update

Silverlight ComboBox loses selection on ItemsSource update


Download

I’ve been beating my head against a number of problems with the Silverlight project I am working on – but one of the worst was the amount of spaghetti you need to write if you are going to use Combo boxes or other ItemsControls whose source may change but you want to persist the current selection.  I figured that there had to be a better way, but I couldn’t find anything on Google.  

Eventually I bit the bullet and hijacked Silverlight’s binding system and replaced the bindings for ItemsControls with my own version that replaces the value after updating the source.  It’s nice because you wire the binding like normal and then just call “Hook” on my class and it does the rest for you! Woohoo, data grids with comboboxes that can update…. That’s going to save me a few of the hours I burned writing this thing.  

You start the hook by defining an instance in your UserControl and calling Hook from the _Loaded event, or anywhere after the tree has been formed and the initial bindings made.  That’s pretty much it – you can supply a delegate if you want to fix up your own list and you can hijack the ItemControls method of selecting to force a search of the list using the Equals override.

_hook.Hook(this); //!!!!!!

Source files here

, , , ,

10 Comments