combobox itemssource binding issue

In a MVVM WPF application, I have a master-detail View that is used to manage Alarms. In the AlarmDetails View I have several properties that are set through ComboBox controls. One of those is the Alarm's Source, and those sources can be changed in another View (SourcesView), thus changing the combobox content in the AlarmDetailsView.

The way the data flows is relatively straightforward

  • A source is added to the collection in SourcesView
  • A message is sent from SourcesView and is received by all AlarmDetailsViewModel
  • The Sources collection in each AlarmDetailsViewModel is changed accordingly and since it is an ObservableCollection it notifies each binding targets of the change
  • The AlarmDetailsView has its DataContext set to a selected AlarmDetailsViewModel, thus the ItemsSource property of its Sources ComboBox is updated (I see it using a dummy converter, in debug I can see the updated collection being passed to ItemsSource, and it contains the newly added item)
  • After all this happened, when I navigate to the AlarmsView/AlarmDetailsView Tab, the items in the Sources ComboBox do not contain the new item.
  • When I select a new Alarm in the master list and I select the original one back, the combobox displays the new item just fine (when i select an item in the master list, the DataContext property of the AlarmDetailsView gets updated).
  • I am at a loss with this, I have been poundering this for a while.

    Anyone have an idea? Any debugging suggestions?

    Thanks,

    Alex

    EDIT: The ComboBox that does not update is located within the AlarmDetailsView, which means that it is used to set a property of the Alarm object (its Source, namely). Hope this helps.

    EDIT 2: In addition to the answer below, another solution was to use an ObservableCollection instead of a List. For some reason, raising a PropertyChange event on the List (as described above) triggered the ComboBox to refresh its ItemsSource, but not to add the new items to its drop-down. Using the ObservableCollection, there is not need to raise the propertychange event since it manages it all by itself.


    ** EDIT **

    Ugly solution but worked for me if someone has a better solution please share :)

    Emptied the collection, notified the GUI, added the real collection notified to gui


    Try using CollectionViewSource to handle your Master/Detail bindings.

    Heres an example

    <Window.Resources>
        <CollectionViewSource x:Key="data" Source="{Binding}" />
    <Window.Resources>
    
    DataContext="{Binding CurrentItem, Source={StaticResource data}}"
    
    链接地址: http://www.djcxy.com/p/41258.html

    上一篇: Silverlight组合框强制重新选择SelectedItem

    下一篇: 组合框itemssource绑定问题