ObservableCollection DataGrid - wpf

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

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

CollectionChanged event of ObservableCollection

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.

List<> collection does not update the View in MVVM

I used the List<Person> collection as the ItemsSource for the DataGrid control.
But it did not update the View if i remove the item from the List collection. I was struggling for a long time for the solution.
Then Instead of the List<Person> collection in my ViewModel. I changed this into ObservableCollection<Person> collection. Now it updates the view when ever there is a change in the collection.
I am not sure why it updates only for ObservableCollection<Person> ? Anyone ?
Well its in the name. A simple List doesn't tell the ui to update, in other words "the view can't observe the list". There is no weird magic behind Databinding. WPF and DataBinding needs the Data model to tell them "this is new" or "this is changed", you propably already saw INotifyPropertyChanged, INotifyCollectionChanged is the same but for collections, and the List<> doesn't implement it, ObservableCollection does.
Because List does not implement INotifyCollectionChanged
Because the update of a databinding is not a kind of magic, there are several requirements to make databinding working correctly. If you have a single property to bind on this property must be either a dependency property or its parent class must implement the INotifyPropertyChanged interface to notify the wpf binding system about changes of the property value.
For a collection there is a simelar mechanism: it must implement INotifyPropertyChanged to inform the wpf binding system about removed/moved/added items.
See here for more details: http://msdn.microsoft.com/en-us/library/system.collections.specialized.inotifycollectionchanged.aspx
ObservableCollection<T> fires change events every time you change the items in the collection. List<T> doesn't. That's the reason.
DataBinding is lazy. If you don't tell your view that something has changed, it won't bother updating. Under the hoods, WPF DataBinding registers for change notifications, so that your ViewModel can tell the view when it has changed. It does this with interfaces like INotifyPropertyChanged and INotifyCollectionChanged.
ObservableCollection<T> implements the interface INotifyCollectionChanged. This interface defines the event CollectionChanged, which your View basically attaches its own event handler to. That handler will update the view when the event is raised by the collection.

Change DataGrid contents in the CellEditEnding event

I have bound the WPF DataGrid to an observable collection of view models, where each view model represents each row in the DataGrid. The view model handles the BeginEdit and CellEditEnding events.
In one of the scenarios, I want to change the contents of the observable collection in the CellEditEnding event. But, I cant do this because the DataGrid is still in edit mode and if I try to add / remove items from the observable collection an exception is thrown and it causes my application to crash.
Any suggestions?
Dispatch it please.
In the CellEditEnding handler invoke your code with the Dispatcher.BeginInvoke() method.
Also you said
The view model handles the BeginEdit and CellEditEnding events.
If you are using pure MVVM then this is forbidden. MVVM implements events via Delegate/Relay Commands.

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.

Resources