5

I have an ObservableCollection binded to a listbox

public ObservableCollection<InsertionVM> Insertions
{
    get
    {
        return _insertions;
    }
    set
    {
        _insertions = value;
        base.OnPropertyChanged("ChromosomeList");
    }
}

Its member, InsertionVM implements INotifyPropertyChanged. It has a property that will be updated.

public bool IsChecked
{
    get
    {
        return _isChecked;
    }
    set 
    {
        _isChecked = value;
        base.OnPropertyChanged("IsChecked");
    }
}

Why doesn't the ObservableCollection refresh even though I implement the INotifyPropertyChanged interface for each property?


Update:

I tried the link given below, but the "more sensitive collection" is only updated when objects are removed / added.

if (e.Action == NotifyCollectionChangedAction.Remove)
{
    foreach (InsertionVM item in e.NewItems)
    {
        //Removed items
        item.PropertyChanged -= EntityViewModelPropertyChanged;
    }
}
else if (e.Action == NotifyCollectionChangedAction.Add)
{
    foreach (InsertionVM item in e.NewItems)
    {
        //Added items
        item.PropertyChanged += EntityViewModelPropertyChanged;
    }
}

public void EntityViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    //Debugger does not reach here
}

Constructor:

public ChromosomeVM(Chromosome _chr, string insertionFilePath)
{
    Chr = _chr;
    _insertions.CollectionChanged += ContentCollectionChanged;
}     
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
Kiang Teng
  • 1,675
  • 4
  • 23
  • 42

3 Answers3

2

Always remember the following:

ObservableCollection<T> only notifies when number of items (it may stay same, when one item is added and one is removed, but you get the point) in it changes.

If an item in ObservableCollection<T> changes, collection is not responsible for propagating change notifications.

decyclone
  • 30,394
  • 6
  • 63
  • 80
2

This is your code: (please see the comment also, made by me)

public ObservableCollection<InsertionVM> Insertions // propertyName == Insertions
{
    get
    {
        return _insertions;
    }
    set
    {
        _insertions = value;
        base.OnPropertyChanged("ChromosomeList"); // What is ChromosomeList??
    }
}

Can you see the problem now? Change ChromosomeList to Insertions. Hope some problem at least will be fixed!

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Didn't notice. That was the problem -_- – Kiang Teng Dec 30 '10 at 12:00
  • Spell it out for thick people like me. Obviously you are saying that the string passed to OnPropertyChanged is significant. What exactly should its value be? The name of the instance of the collection? – Peter Wone Oct 21 '11 at 05:14
  • @PeterWone: No. The name of the *property*, not of the *instance*. In this example, the instance is `_insertions`, but the property is `Insertions`. – Nawaz Oct 21 '11 at 05:27
0

be sure to put the [Insertions] in the path of the binding and it will work

[Insertions] will be updated only if you change the reference like.

Insertions = new ObservableCollection<InsertionVM>( Items);

To make your code more effective add check if the value change in the set like

public ObservableCollection<InsertionVM> Insertions
    {
        get
        {
            return _insertions;
        }
        set
        {
       if(_insertions != value)
           {
               _insertions = value;
               base.OnPropertyChanged("Insertions");
           }
        }
    }
Waleed A.K.
  • 1,596
  • 13
  • 13