开发者

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

  1. A source is added to the collection in SourcesView
  2. A message is sent from SourcesView and is received by all AlarmDetailsViewModel
  3. The Sources collection in each AlarmDetailsViewModel is changed accordingly and since it is an ObservableCollection it notifies each binding targets of the change
  4. 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)
  5. After all this happened, when I navigate to the AlarmsView/AlarmDetailsView Tab, the items in the Sources ComboBox do not contain the new item.
  6. 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, w开发者_高级运维hich 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}}"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜