CollectionChanged event of ObservableCollection - wpf

I have an ObservableCollection, this collection has 2 items (model), the model has Value as a property.
there is an event CollectionChanged for the collection, which suppose to fire when an item is changed, so I am expecting to see this event get fire when a model Value sets but I don't how model should be structured to fire that event?
I know that Model can have an event and when a Model is added a handler can be assigned to this event, but I want to know how CollectionChanged works for change of item in the collection?

CollectionChanged will only be raised when a model replaces another in your collection. Property changes to a model that is already inside the collection will not raise it.
You will need to handle those with the INotifyPropertyChanged.PropertyChanged event, which your models must expose.

It won't fire if a property within the element is changed, only if you assign a new "model" item to an index of the collection.
If you want WPF to update when you change a property value within your Model class, you need to make the model instances implement INotifyPropertyChanged.

A hack is to add a new element to the collection and remove it immediately. That will raise the collectionchanged event.

Related

How DataGrid is notified when item in the collection is changed?

I have two DataGrids bound to the same ObservableCollection property in my ViewModel.
When I edit an existing row in one DataGrid another gets updated after row is commited.
But how it works? The NotifyCollectionChanged event is not raised on the underlying ObservableCollection. I have checked it by subscribing to the event.
In this case, you are modifying the properties of the item (entity) of the collection, not the collection itself.
Therefore, the CollectionChanged event is not raised.
To update properties in an entity, INotifyPropertyChanged must be implemented in it.
But if the change of properties happens ONLY through bindings, then even this is not necessary.
Bindings use PropertyDescriptor and other types of reflection in their work.
This allows bindings to "know" that a property has been changed by another binding.
If you speak Russian, read my article: https://www.cyberforum.ru/wpf-silverlight/thread2650880.html

WPF telerik GridView aggregate is not updating until focus out of a grid view field

telerik:GridView, using aggregate function to show sum in footer, the property is being updated from somewhere else and GridView is not calling PropertyChanged until focus out from GridView Column.
How we can refresh aggregate as soon as Item-source Property is changed.
I want to get aggregate updated as soon as ItemSource Property is updated.
On GridView property change I am using
private void GV_PropertyChanged(object s, PropertyChangedEventArgs e)
{
GV.CalculateAggregate();
}
According to the docs, it seems like you should raise the CollectionChanged event for the source collection:
In order to get the aggregate results refreshed, you have to notify
the GridView that the bound collection has been changed. A
CollectionChanged notification of the bound collection should be
raised.
There are four possible solutions:
You can update the value as illustrated in this help article on how to
edit an item outside RadGridView.
Ensure that a CollectionChanged event is raised from the bound source
collection.
Invoke RadGridView.CalculateAggregates() method.
Invoke RadGridView.Rebind() method. It will raise CollectionChanged
notification with action Reset. Please note that the entire view will
be recreated.

how to track changes to PageCollectionView SortDescriptions

When a DataGrid is bound to a PagedCollectionView the user can resort the data of a given column or collection of columns. This user action manipulates the SortDescriptions collection and when that happens I need to refresh the underlying data with a new query from the server.
Ideally I would attach an event handler to CollectionChanged event of the SortDescriptions property, but I can't since it's is protected.
What then is the correct method for tracking changes to the SortDescriptions collection of the PagedCollectionView?
Turns out it's a simple matter of casting the SortDescriptions property to an INotifyCollectionChanged which will expose the CollectionChanged event.
((INotifyCollectionChanged)Data.SortDescriptions).CollectionChanged += (s,e)=> { ... };

WPF MVVM: Notifying the View of a change to an element within an ObservableCollection?

Calling OnPropertyChanged for an ObservableCollection only works when there has been some change to the properties of the collection, not the objects it contains (add, remove, clear, etc).
Is there any way to notify the View that there has been a change to an item within the collection?
The objects it contains have to implement INotifyPropertyChanged as well. In your setter you trigger the event, and WPF will pick up on this and read in the new value as long as you are using two-way bindings or read-only bindings.

ObservableCollection DataGrid

I bound the ObservableCollection to the dataGrid itemssource.
the collectionChangedEvent of the observable Collection is getting called only when we add, delete, remove. But not firing when we update the record.
how to fire the event for Update too?
If you want to be notified when an item is changed (ie you want to subscribe to this event), you are out of luck with ObservableCollection<T> because this collection only fires the CollectionChangedEvent.
Indeed, if you implement INotifyPropertyChanged, you will see changes to the items in the view (WPF does this automatically), but if you need to execute manual actions when an item changes, you can use BindingList<T>.
For exactly this scenario I rolled out a custom BindableCollection<T>, which implements ObservableCollection<T> and adds the OnItemChangedEvent. I can provide some sample code if necessary...
The collection doesn't know when the record is modified. To get a notification when this happens, the record needs to implement INotifyPropertyChanged

Resources