views:

665

answers:

3

I have a WPF CombBox as follows:

<ComboBox 
   ItemsSource="{Binding Source={StaticResource myList}}"
   SelectedItem="{Binding Path=mySelectedItem}"
/>

The problem I have is that when the bound value changes, the selected value in the combobox's textbox does not update. (Note - the values in the combobox list do update).

I am using MVVM so I can detect in the view model when the binding changes and call a property changed event and this is updating the combobox, but not the value displayed within the textbox.

I think this could be done in the template of the combobox - somehow make the textbox be bound to the selecteditem of the combobox, or always update when it updates?

EDIT:

I didn't make clear - I do implement INotifyPropertyChanged properly and when the value changes I raise PropertyChanged for myList and mySelectedItem. The problem is that the textbox within the combobox is not refreshing.

I found a similar question:

http://stackoverflow.com/questions/1605939/wpf-combobox-selecteditem-not-updating

Which gives hints of an answer but not enough unfortunately.

A: 

Most probably your mySelectedItem is not a dependency property. Or, alternatively, the containing class doesn't implement INotifyPropertyChanged.

Consider the case when your value is neither a dependency property nor a property of an INotifyPropertyChanged. In this case, WPF doesn't have a chance to know that its value changed, therefore the value is loaded only once (using reflection) at the beginning.

For the case of dependency property, or when the containing object supports INotifyPropertyChanged, the WPF framework can subscribe to the value changes and thus update the combobox.

Vlad
A: 

If your expecting the selected item to change when the value behind your 'myList' collection changes it wont. If your using MVVM and you are implementing INotifyPropertyChanged in the view model. Then when you are raising the PropertyChanged for myList, you should also be raising it for 'mySelectedItem' if you are expecting it to rebind or change in any way.

Leigh Shayler
+1  A: 

I have updated my answer for you here

Hope it helps!!

viky
Thanks Viky, most appreciated.
WillH