Gotcha: SelectedItem in a ComboBox doesn’t get set

This blog post is about why the SelectedItem-property of a ComboBox might not get set in XAML declarative binding.

I had an issue with the ComboBox when developing a Windows Store App. I was using MVVM Light and everything looked fine. The ViewModel looked something like this.

And the declarative binding in XAML looked something like this.

Pretty straightforward. But the ComboBox default value (or selected item) never got set. It drove me bonkers.

Here’s the thing: it matters in what order properties in XAML are set. The ItemsSource-property needs to be set before SelectedItem.

The SelectedItem must also be an item from the ItemsSource.

So the correct way to declare the ComboBox in XAML would be.

And you should be good to go.

daniel
daniel
Developer
Recommended Posts
Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt

Start typing and press Enter to search