6

Can someone point to an example where CollectionChanged is implemented. I am using wpf mvvm light. I tried to google, didn't find anything good enough.

ns12345
  • 3,079
  • 5
  • 41
  • 62
  • A little more detail, please. What are you trying to do? – Wonko the Sane Jan 03 '11 at 19:27
  • I have a datagrid with observableCollection itemsource that has inline editing enabled, when the user is done editing/adding/removing the data in this datagrid i want to send that data to webservice to update. – ns12345 Jan 03 '11 at 19:54
  • 1
    I can't believe you've accepted Arxisos answer below... the code below makes no sense... How to implement NotifyCollectionChanged and NotifyCollectionChangedEventArgs? Take a look here http://blog.stephencleary.com/2009/07/interpreting-notifycollectionchangedeve.html for a good short descriptions and there are also links to implementations and further details. – Beachwalker Aug 19 '14 at 16:27

2 Answers2

11
public ObservableCollection<string> Names { get; set; }

public ViewModel()
{
   names = new ObservableCollection<string>();
   Names.CollectionChanged += this.OnCollectionChanged;
}

void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
   //Get the sender observable collection
   ObservableCollection<string> obsSender = sender as ObservableCollection<string>;

   List<string> editedOrRemovedItems = new List<string>();
   foreach(string newItem in e.NewItems)
   {
       editedOrRemovedItems.Add(newItem);
   }

   foreach(string oldItem in e.OldItems)
   {
       editedOrRemovedItems.Add(oldItem);
   }

   //Get the action which raised the collection changed event
   NotifyCollectionChangedAction action = e.Action;
}

For more information about the NotifyCollectionChangedEventArgs look here.

EDIT: Because you need a list of added/removed items, I modified the sample code.

Edward
  • 5,942
  • 4
  • 38
  • 55
Arxisos
  • 2,719
  • 20
  • 21
  • for some reason, the onCollectionChanged method gets invoked only in the case of add or remove items. but not in the case of edit item. I would also like to know how to go forward with this, I mean how to finally send the list/collection of newly added, removed and edited items in a single collection. Thanks. – ns12345 Jan 03 '11 at 19:53
  • Sounds like you have another question to post. – Joe White Jan 03 '11 at 20:00
  • 1
    ObservableCollection doesn't provide features for calling OnCollectionChanged on editing elements. You have to do that yourself. Your items in the collection must implement INotifyPropertyChanged and you have to add event listeners to INotifyPropertyChanged's PropertyChanged event. Look at http://stackoverflow.com/questions/269073/observablecollection-that-also-monitors-changes-on-the-elements-in-collection to get more information. – Arxisos Jan 03 '11 at 21:07
  • If you have problems with that: Create a new question, because this doesn't belong to this question. – Arxisos Jan 03 '11 at 21:16
2
 public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

    }
    private ObservableCollection<Person> persons = new ObservableCollection<Person>();


    public MainWindow()
    {
        InitializeComponent();
        dgPerson.ItemsSource = persons;
        persons.CollectionChanged += this.OnCollectionChanged;
    }


    void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        //Get the sender observable collection
        ObservableCollection<Person> obsSender = sender as ObservableCollection<Person>;
        NotifyCollectionChangedAction action = e.Action;
        if (action == NotifyCollectionChangedAction.Add)
            lblStatus.Content = "New person added";
        if (action == NotifyCollectionChangedAction.Remove)
            lblStatus.Content = "Person deleted";
    }
    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        Person p = new Person();
        p.FirstName = txtFname.Text;
        p.LastName = txtLname.Text;

        persons.Add(p);
    }

A very simple complete example here

Alin
  • 21
  • 2