Jaco Briers
Jaco Briers

Reputation: 1715

Why does the SelectedItem not display in combobox

With the below binding in XAML, the selected item is not displayed in a combobox (specifically, the current type is not displayed for a site). We are using the caliburn micro framework and DevForce Ideablade. MySite is an Ideablade entity.

<ComboBox Grid.Column="3" Grid.Row="3" 
          ItemsSource="{Binding MySiteTypes}"
          DisplayMemberPath="description"
          SelectedItem="{Binding MySite.SiteType, Mode=TwoWay}"
          SelectedValuePath="description"/>

Code in the ViewModel:

    private BindableCollection<SiteType> _mySiteTypes;
    public BindableCollection<SiteType> MySiteTypes
    {
        get { return _mySiteTypes; }
        set { _mySiteTypes = value; NotifyOfPropertyChange(() => MySiteTypes); }
    }

Once I set the site type with the combobox (the types are present in the dropdown), it correctly displays the type and changes the type in the database. What am I missing, why doesn't it want to display the existing type the first time?

Upvotes: 2

Views: 3006

Answers (3)

Jaco Briers
Jaco Briers

Reputation: 1715

Ok, I found the solution (Myles J was close):

The selected item is evaluated before the items from the itemsource were loaded causing the selected item not to be displayed. I needed to call NotifyOfPropertyChange for MySite AFTER the async query completed that populates MySiteTypes.

Upvotes: 2

Myles J
Myles J

Reputation: 2880

From my experience with the ComboBox these kind of issues often boil down to timing. Is your ItemSource (MySiteTypes) available before the SelectedItem binding is evaluated?

Upvotes: 2

zapico
zapico

Reputation: 2396

Are the items loaded? Maybe you'll have to use a DataTemplate to show their text property.

If they are strings, I had a similar problem recently and had to use the selectedIndex by using a converter:

Converter for ComboBox in Silverlight 4

Upvotes: 0

Related Questions