I’m using ListBox with custom ItemsSource:
this.ListOfPersonsListBox.ItemsSource = (List<Person>)ListOfPersons.AllPersons;
ListOfPersons is a static class so it cannot implement INotifyPropertyChanged nor IObservableCollection.
What’s the simplest way to redraw my ListBox after updating the list? My current code works, but I would like to find a cleaner solution:
private void SyncButton_Click(object sender, EventArgs e)
{
ListOfPersons.Sync();
this.ListOfPersonsListBox.ItemsSource = null;
this.ListOfPersonsListBox.ItemsSource = ListOfPersons.AllPersons;
}
Consider using an ObservableCollection instead of a
List. It implementsINotifyPropertyChangedinternally. You can loop through your list and add each element to a newObservableCollectionobject and bind this to theListBox.If you’re going to convert often, you could just create an
Extension method