Binding to ObservableCollection not working RRS feed

  • Question

  • I have the following code:


            <Button Content="Add Value" Command="{Binding AddValue}"/>
            <TextBlock Text="Values:"/>
            <ListBox ItemsSource="{Binding Values}"/>

    partial class MainPage
            public MainPage()
                DataContext = new MainPageViewModel();



    public class MainPageViewModel : INotifyPropertyChanged
            private ObservableCollection<string> _values = new ObservableCollection<string>() { "alpha", "beta", "gamma" };
            public ObservableCollection<string> Values { get { return _values; } }
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged( string name )
                if( null != PropertyChanged )
                    PropertyChanged( this, new PropertyChangedEventArgs( name ) );
            public ICommand AddValue { get; private set; }
            public MainPageViewModel()
                AddValue = new RelayCommand( OnExecuteAddValue );
            private void OnExecuteAddValue( object context )
                Values.Add( "meow" );



    I can verify that ExecuteAddValue is being called (hits breakpoint) and the Values collection is being updated with additional members, but the binding does not appear to be getting updated.  I remember reading something earlier about ObservableCollection bindings not working and there being a workaround.  Can anyone point me to that post?  

    • Edited by Robert Zhu Tuesday, October 4, 2011 9:38 AM
    Tuesday, October 4, 2011 9:38 AM


All replies

  • ObservableCollection<T> is using the other INotifyPropertyChanged. Oh, the mess. Not sure what's the story with INotifyCollectionChanged.

    Miha Markic [MVP C#] http://blog.rthand.com
    • Edited by Miha Markic Tuesday, October 4, 2011 10:38 AM
    Tuesday, October 4, 2011 10:37 AM
  • Hi Miha, yes this is a known issue with two INPC interfaces.  INCC has some known issues in the preview.  If you look at the databinding SDK sample you'll see a helper class for ObservableVector<T> that should help in some cases http://code.msdn.microsoft.com/windowsapps/Data-Binding-7b1d67b5
    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer
    • Marked as answer by Robert Zhu Tuesday, October 4, 2011 5:18 PM
    Tuesday, October 4, 2011 3:46 PM
  • As others have stated, the issue is that WinRT uses IObservableVector, whereas ObservableCollection exposes INotifyCollectionChanged. To solve the problem I have created a little adapter that you can use within a value converter as follows:

    <ItemsControl ItemsSource="{Binding Path=MyCollection, Converter={StaticResource ObservableCollectionAdapter}}"/>

    You can read about it here:


    Regards, Colin E.

    Thursday, October 6, 2011 7:40 AM
  • thanks Tim!


    Now taking this example if any of viewmodels are moved out of the MainPage.xaml.cs file then the databinding doesn't work.  Is this a known issue?


    For instance if you move Employee out into a separate .cs file then the databinding doesn't work.

    Monday, October 24, 2011 7:02 PM