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.

3
  • A little more detail, please. What are you trying to do? Jan 3, 2011 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 3, 2011 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 blog.stephencleary.com/2009/07/… for a good short descriptions and there are also links to implementations and further details. Aug 19, 2014 at 16:27

2 Answers 2

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.

4
  • 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 3, 2011 at 19:53
  • Sounds like you have another question to post.
    – Joe White
    Jan 3, 2011 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 stackoverflow.com/questions/269073/… to get more information.
    – Arxisos
    Jan 3, 2011 at 21:07
  • If you have problems with that: Create a new question, because this doesn't belong to this question.
    – Arxisos
    Jan 3, 2011 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.