Title: ComboBox bound to ICollectionView is displaying incorrect SelectedItem
Tags: silverlight;mvvm;combobox;icollectionview
Question: I'm having an issue with a pair of combo box's in Silverlight 4.0.

The intention is to have two different comboboxes that read from the same list, but if any item selected in one won't show up in the other (as the underlying properties are not allowed to be the same).

E.g. (this is just example code, but identically represents how it works)

```<ComboBox ItemsSource="{Binding BackgroundColors}"
          SelectedItem="{Binding SelectedBackgroundColor, Mode=TwoWay}" /&gt;

<ComboBox ItemsSource="{Binding ForegroundColors}"
          SelectedItem="{Binding SelectedForegroundColor, Mode=TwoWay}" /&gt;
```

To allow for this dynamic filtering, I have 2 different ```ICollectionView```'s in my ViewModel that each combo box ```ItemsSource``` is bound to. Each ```ICollectionView``` has a source of the same ```ObservableCollection<T&gt;``` but in the filter is set to filter out the other's selected item.

```private ObservableCollection<Color&gt; _masterColorList;         
public ICollectionView BackgroundColors { get; }
public ICollectionView ForegroundColors { get; }
```

When a SelectedItem is changed in the UI, the ViewModel properties are updated, and as part of that, the opposite ```ICollectionView``` is refreshed via ```.Refresh()```. 

Eg.

```public Color SelectedForegroundColor
{
    get { return _selectedForegroundColor; }
    set
    {
        if (_selectedForegroundColor == value)
            return;

        _selectedForegroundColor = value;

        BackgroundColors.Refresh();

        RaisePropertyChanged(() =&gt; SelectedForegroundColor);
    }
}
```

This allows the filter to rerun and change what is available to be selected.

This works pretty well, but there is a problem:

Say we have 3 colors in our master list:


Blue 
Green
Red


Combo box 1 (CB1) is has selected Blue
Combo box 2 (CB2) has selected Green

Thus the combo boxes have these lists (bold is selected)

CB1


Blue  
Red  


CB2


Green  
Red  


If I then select Red in CB1, I would expect that Red would be removed from CB2 and Blue replace it. This happens correctly, BUT the displayed value changes from Green to Blue.

The underlying bound value doesn't get changed and the ICollectionView.CurrentItem is correct, but the display is clearly showing the wrong value. 

What I think is happening is that because Green is earlier in the list, that it is getting confused with what is being shown. It also occurs if you are sorting the ICollectionView.

I've tried re-raising the property changed event notification for the changing combobox &amp; selected item but this doesn't seem to work.

Has anyone seen this issue before or any ideas how I could fix it?



Here is another answer: There are at least 5 serious issues with binding of comboboxes.

Here I think you ran into 

http://connect.microsoft.com/VisualStudio/feedback/details/523394/silverlight-forum-combobox-selecteditem-binding

this one.

Once you update itemssource binding stops working.

I used one of solutions available there, this code solved this issue for me:

```public class ComboBoxEx : ComboBox
    {
        #region Fields

        private bool _suppressSelectionChangedUpdatesRebind = false;

        #endregion

        #region Properties

        public static readonly DependencyProperty SelectedValueProperProperty =
            DependencyProperty.Register(
                "SelectedValueProper",
                typeof(object),
                typeof(ComboBoxEx),
                new PropertyMetadata((o, dp) =&gt; {
                                          var comboBoxEx = o as ComboBoxEx;
                                          if (comboBoxEx == null)
                                              return;

                                          comboBoxEx.SetSelectedValueSuppressingChangeEventProcessing(dp.NewValue);
                                      }));

        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
        public object SelectedValueProper
        {
            get { return GetValue(SelectedValueProperProperty); }
            set { SetValue(SelectedValueProperProperty, value); }
        }

        #endregion

        #region Constructor and Overrides

        public ComboBoxEx()
        {
            SelectionChanged += ComboBoxEx_SelectionChanged;
        }

        /// <summary&gt;
        /// Updates the current selected item when the <see cref="P:System.Windows.Controls.ItemsControl.Items"/&gt; collection has changed.
        /// </summary&gt;
        /// <param name="e"&gt;Contains data about changes in the items collection.</param&gt;
        protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            // Must re-apply value here because the combobox has a bug that 
            // despite the fact that the binding still exists, it doesn't 
            // re-evaluate and subsequently drops the binding on the change event
            SetSelectedValueSuppressingChangeEventProcessing(SelectedValueProper);
        }

        #endregion

        #region Events

        private void ComboBoxEx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Avoid recursive stack overflow
            if (_suppressSelectionChangedUpdatesRebind)
                return;

            if (e.AddedItems != null &amp;&amp; e.AddedItems.Count &gt; 0) {
                //SelectedValueProper = GetMemberValue( e.AddedItems[0] );
                SelectedValueProper = SelectedValue; // This is faster than GetMemberValue
            }
            // Do not apply the value if no items are selected (ie. the else)
            // because that just passes on the null-value bug from the combobox
        }

        #endregion

        #region Helpers

        /// <summary&gt;
        /// Gets the member value based on the Selected Value Path
        /// </summary&gt;
        /// <param name="item"&gt;The item.</param&gt;
        /// <returns&gt;</returns&gt;
        private object GetMemberValue(object item)
        {
            return item.GetType().GetProperty(SelectedValuePath).GetValue(item, null);
        }

        /// <summary&gt;
        /// Sets the selected value suppressing change event processing.
        /// </summary&gt;
        /// <param name="newSelectedValue"&gt;The new selected value.</param&gt;
        private void SetSelectedValueSuppressingChangeEventProcessing(object newSelectedValue)
        {
            try {
                _suppressSelectionChangedUpdatesRebind = true;
                SelectedValue = newSelectedValue;
            }
            finally {
                _suppressSelectionChangedUpdatesRebind = false;
            }
        }

        #endregion
    }
```

Its not mine code, but one from articles related to this bug.

Comment for this answer: doh. Well I can recommend not to bind to ICollectionView maybe and try something easier like IEnumerable instead (for starters), I ran into big bug with CollectionViewSource here http://stackoverflow.com/questions/6305608/how-to-preserve-twoway-binding-of-currentitem-when-databinding-to-collectionview There are plenty problems with combobox bindings sadly. Ill think of other possible causes.
Comment for this answer: binding to combobox in silverlight is like the worst thing I encountered in my programming life. too many issues.. and not fixed yet. Its also annoying because you totally don't expect it to be so buggy.
Comment for this answer: Unfortunately  this, or any of the other workarounds at that connect site, didn't seem to work. I believe the SelectedItem/SelectedValue binding is still working, but the visual display on the Combobox is incorrect.
Comment for this answer: Yeah, in a newer revision of our code, we have made the change back to ObservableCollection. Just a super annoying bug :/

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:11/02/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<silverlight><mvvm><combobox><icollectionview>', 'Title': 'ComboBox bound to ICollectionView is displaying incorrect SelectedItem', 'ViewCount': 744, 'id': 7976062, 'user_AboutMe': '<p>Developer. Engineer. Enthusiast. Goodjer. Bugler.</p>\n', 'user_AccountId': 41387.0, 'user_DisplayName': 'Alastair Pitts', 'user_Reputation': 19155.0, 'user_Views': 1891.0, 'user_id': 120243.0}}