简体   繁体   中英

WPF update View for list with INotifyPropertyChanged

I know that the actual way is to use an ObservableCollection . I want to teach someone WPF and started with an ItemsControl which binds to a List<MyPoco> . Now I'm implementing a RemoveCommand . What I've planned to do is something like that:

private void Remove(MyPoco obj) 
{
    if (MyList.Contain(obj)) 
    {
        MyList.Remove(obj);
        OnPropertyChanged(nameof(MyList));
    }
}

I'd have expected WPF to update the View. Why isn't that working?

I'm implementing INotifyPropertyChanged the proper way. It's working for other properties.

You notify that the MyList property has changed, but the View did realize that it already displays that instance stored in MyList property and so there is no update.

You can try this code

private void Remove(MyPoco obj) 
{
    MyList = MyList.Where( e => e != obj ).ToList();
    OnPropertyChanged(nameof(MyList));
}

Now there is a new list instance and the view will update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM