Jordan
Jordan

Reputation: 9911

ComboBox not changing when bound value of SelectedItem changes

I have a ComboBox that has its ItemSource and SelectedItem properties bound to a view model. And I have the following block of code that is the callback for a data query against a DomainContext:

    /// <summary>
    /// Stores (readonly) - Stores available for ship to store.
    /// </summary>
    public ObservableCollection<StoreEntity> Stores
    {
        get { return _stores; }
        private set { _stores = value; RaisePropertyChanged("Stores"); }
    }

    /// <summary>
    /// SelectedStore - Currently selected store.
    /// </summary>
    public StoreEntity SelectedStore
    {
        get { return _selectedStore; }
        set { _selectedStore = value; RaisePropertyChanged("SelectedStore"); }
    }

    /// <summary>
    /// When stores are completely loaded.
    /// </summary>
    /// <param name="a_loadOperations"></param>
    protected void OnStoresLoaded(LoadOperation<StoreEntity> a_loadOperations)
    {
        Stores.AddRange(a_loadOperations.Entities);
        SelectedStore = a_loadOperations.Entities.FirstOrDefault();
    }

In this example, Stores is a ObservableCollection<StoreEntity> (AddRange is an extention method) and is bound to ItemSource, and SelectedStore is a StoreEntity and is bound to SelectedItem.

The problem here is that the ComboBox is not changing its selection to reflect the change in SelectedItem.

Edits:

I've even tried the following, though I think that a_loadOperation.Entities is already a realized set:

    /// <summary>
    /// When stores are completely loaded.
    /// </summary>
    /// <param name="a_loadOperations"></param>
    protected void OnStoresLoaded(LoadOperation<StoreEntity> a_loadOperations)
    {
        var entities = a_loadOperations.Entities.ToArray();
        Stores.AddRange(entities);
        SelectedStore = entities.First();
    }

Thanks

Upvotes: 0

Views: 1011

Answers (2)

Jordan
Jordan

Reputation: 9911

I had SelectedItem bound to Stores instead of SelectedStore. Ooops!

Upvotes: 0

Adam Jones
Adam Jones

Reputation: 721

If you are trying to get a change to your viewmodel (specifically the SelectedStore property) to be reflected in your combo box, you could:

  1. Confirm the binding worked. Check it was set up properly in the XAML, and check the Output window to see if there is a message saying the Binding failed
  2. Confirm your DataContext is set properly (it probably is since you are getting the combo box `ItemsSource` from `Stores`
  3. Subscribe to your PropertyChanged event and confirm that it is being raised when the property changes

If that doesn't work, it may be a bug with the ComboBox. I have seen cases where the order of specifying the properties in XAML makes the difference (ex: you should set ItemsSource first and SelectedItem second). I have also seen a binding fail until I added Mode=TwoWay (even though in your example you are trying to get the binding to update from your view model to your UI). Try confirming that your ComboBox XAML is like this: <ComboBox ItemsSource="{Binding Stores}" SelectedItem="{Binding SelectedStore, Mode=TwoWay}" /> Order shouldn't matter since XAML is declarative, but I have personally seen it matter with ComboBoxes in Silverlight.

Upvotes: 1

Related Questions