I think both are same, but why are both used in the same method? I think there is a minor difference. Here is some code to show the difference between the two:
private void LoadItemListing()
{
_items = new ObservableCollection<SalesItemListingViewModel>();
foreach (ItemListing x in _sales.Items)
{
SalesItemListingViewModel itemListing = new SalesItemListingViewModel(x);
_items.Add(itemListing);
_itemAmountSum += itemListing.Amount;
itemListing.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(itemListing_PropertyChanged);
}
_items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_items_CollectionChanged);
}
And for itemListing_PropertyChanged:
void itemListing_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "Amount")
{
ItemAmountSum = 0;
foreach (SalesItemListingViewModel x in Items)
ItemAmountSum += x.Amount;
}
}
And this code for _items_CollectionChanged:
void _items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
SalesItemListingViewModel newItemListingViewModel = e.NewItems[0] as SalesItemListingViewModel;
newItemListingViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(itemListing_PropertyChanged);
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
{
ItemAmountSum = 0;
foreach (SalesItemListingViewModel x in Items)
ItemAmountSum += x.Amount;
}
RaisePropertyChanged("Items");
}
I think there is a difference, but I am not sure. Can somebody please explain any difference?
The
PropertyChangedsignals that the value of a property has changed. TheCollectionChangedevent signals that the content of a collection has changed (not the collection itself: it’s still the same collection instance, but elements have been added/removed/replaced).