0

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" ... />
Yael
  • 1,566
  • 3
  • 18
  • 25
Milkncookiez
  • 6,817
  • 10
  • 57
  • 96
  • 2
    You don't need `OnCollectionChanged` in `Multimedia` and `ObservableCollection` already implements `INotifyCollectionChanged`. Seems problem is somewhere else. Can you show your XAML and bindings for `ListBox`? – dkozl Nov 14 '14 at 11:54
  • @dkozl - I updated my question post. – Milkncookiez Nov 14 '14 at 11:59
  • 2
    When you add you use same instance of `mediaList`? Do you add asynchronously? – dkozl Nov 14 '14 at 12:01
  • No, I don't have async methods. I did not understand your comment fully. You mean when I add in the `addMedia()` method? Is what you mean, that I add the new entry to a different instance of the `mediaList` collection? – Milkncookiez Nov 14 '14 at 12:09
  • What I mean is constructor the only place where you do `mediaList = new MultiMediaList();` and later on you just do `mediaList.addMedia(...)`. Can you show how you add item as well? – dkozl Nov 14 '14 at 12:12
  • Yeah, I found my mistake. You were right. I have a separate window that opens, which has the fields for input and creation of new entry to the `mediaList`, and in the code-behind of this 2nd window I was creating a new instance of the collection. This was really not a problem that I was looking for. It is working fine now. Thank you very much! – Milkncookiez Nov 14 '14 at 12:15

0 Answers0