Sara Gamage
Sara Gamage

Reputation: 372

Silverlight 4 MVVM ComboBox data binding is not being displayed

I have a ComboBox with the following XAML

<ComboBox Name="CompanyComboBox"
    ItemsSource="{Binding Path=CompanyList, Mode=OneWay}" 
    SelectedItem="{Binding Path=CurrentCompany, Mode=TwoWay}"
    DisplayMemberPath="Name" />

Problem:

The selected option on 'company' is persisted, but never gets displayed on load. What's missing or going wrong, or what have I forgotten to do?

I have tried the following suggestions, that have so far not solve the issue:

  1. Two-way bind a combobox to a simple string array Order of ItemSource and SelectedValue properties on were correct
  2. ComboBox.SelectedValue not updating from binding source alternating between 'SelectedValue' and 'SelectedIndex' - neither works
  3. Silverlight 4 Combobox with selectedValue using MVVM-Light raising PropertyChanged before setting new value also didn't help
  4. Adding/Removing 'IsEnabled="{Binding IsReady}' on the ComboBox didn't help either
  5. Adding SelectedValuePath="Name" or ="Value" stopped the save from working

Upvotes: 2

Views: 1196

Answers (4)

Andrey S
Andrey S

Reputation: 71

Can we take a look at the view model please? Until this information is not present, may give following suspections.

  1. CurrentCompany property is not public or is not a property.
  2. View model doesn't implement INotifyPropertyChanged interface.
  3. Setter of CurrentCompany property doesn't contain PropertyChanged event notification.

Upvotes: 0

alf
alf

Reputation: 18550

You can try this: after populating CompanyList in your ViewModel, set the CurrentCompany to the first company, or a dummy item that says , or depending on your context.

Upvotes: 0

Chris Wood
Chris Wood

Reputation: 131

Check that the instance assigned to CurrentCompany is the actual one contained in CompanyList and not a duplicate of it.

Upvotes: 1

Rachel
Rachel

Reputation: 132658

You need to overwrite the Company.Equals() method to return true if the object's data is the same.

By default, it only returns true if the two company objects being compared share the same spot in memory, and I am guessing that your CurrentCompany object does not point to an object in CompanyList, so the SelectedItem is being set to null

Upvotes: 3

Related Questions