开发者

ObservableCollection does not refresh even if INotifyProperty interface is implemented

I have an ObservableCollection binded to a listbox

public ObservableCollection<Insertion开发者_如何学CVM> 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;
}     


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!


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.


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");
           }
        }
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜