Implement OnCollectionChanged so that ListBox updates automatically WPF

170 views Asked by At

I have this object wrapper, whose instances I populate the collection with:

public class Multimedia : INotifyPropertyChanged
{
   //... constructor
   //... getters and setters for the properties

   public void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
   {
      ObservableCollection<Multimedia> objSender = sender as ObservableCollection<Multimedia>;
      NotifyCollectionChangedAction action = e.Action;
   }
}

The collection:

public class MultiMediaList : ObservableCollection<Multimedia>
{
    //... constructor with creating several default objects of Multimedia

    public void addMedia(string title, string artist, string genre, MediaType type)
    {
        this.Add(new Multimedia(title, artist, genre, type));
    }
}

So, I have a ListBox that is bound to the collection. When I start up the app, the default values are shown. But when I add a new entry to the collection - the ListBox does not get updated with the new item.

I guess I have not implemented correctly the OnCollectionChanged method, but I cannot grasp an idea of how to do it, from the examples that I have seen online, because every case is so different.

EDIT: Code-behind binding of the Collection and the ListBox:

public partial class MainWindow : Window
{
    MultiMediaList mediaList;
    public MainWindow()
    {
        InitializeComponent();
        mediaList = new MultiMediaList();
        LB_media.ItemsSource = mediaList;
    }

    //...
}

XAML:

<ListBox Name="LB_media" DisplayMemberPath="Title" ... />
0

There are 0 answers