简体   繁体   中英

Why is my listView not updating after changing itemsource?

I have a listview which receives information from a HashSet, but when I delete one item of the HashSet, my listview is not updated.

And my listview doesn't have a method of refresh, don't know why. Here is my code:

private void deleteActivityFromAlumn(String activityName, String nif)
{
    Alumn alumnDelete = Alumn.findAlumnByNIF(nif);
    Activity activityDelete = Activity.getActivityByName(activityName);
    Debug.WriteLine(alumnDelete.Name + activityDelete.Name);
    alumnDelete.activities.Remove(activityDelete);
    activityDelete.Alumns.Remove(alumnDelete);
    listActivities.ItemsSource = alumnDelete.activities;
}

And the item is deleted in a right way because if I go search for the object again, it is removed from the listView, but I believe that its supposed to be updated when you refresh the ItemsSource.

To make sure that control with bound items changes when those items change you have to

  1. Either make that collection implement INotifyCollectionChanged . The simplest way to do it is to use ObservableCollection<T> instead of HashSet<T>
    • Though you will lose the uniqueness property that HashSet<T> gives you.
  2. Or to somehow force a refresh of that control when you change the collection. Though one has to take into account that it is not the best solution , as it will more often than not cause a full redrawing of the control with obvious performance and/or display issues .
    • Either by calling the Refresh method ( WPF , WinForms )
    • Or by re-binding the collection
listActivities.ItemsSource = null; 
listActivities.ItemsSource = alumnDelete.activities

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