NotificationObject update all properties on reference re-assignment - wpf

When your view entity derives from NotificationObject and each property calls the "RaiseProperty" change in its setter, will all properties call "RaiseProperty" on object reassignment. Or do you have set each property manually when you want to notify WPF controls of property changes.
Ex.
Item/entity arrives from WCF.
Item that is already in an observable collection detects that the arrived item is already contained in the collection and should check for any properties that are different between item already in collection and item that arrived from WCF.
Can I do this?
EntityInCollection = EntityFromWCF and have each property raise its event? Or do I have to do this:
EntityInCollection.Property1 = EntityFromWCF.Property1... etc. for each property.
I have a gridview diplaying object and would like to animate the cells that get updated but I'm thinking that reassigning the reference will raise all events and not simply the ones that have changed/are different between the two objects.
Thanks

If you're replacing the entire object, the ObservableCollection will raise an event to notify of this change. UI components will (or should - I can't vouch for all controls, especially 3rd party ones) respond and refresh automatically.

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

How can I determine if my BindingSource's List has changed?

I have a BindingSource which I use to bind to a grid. The binding source itself binds to a custom class. For instance
MyGrid.DataSource = MyBindingSource
'Bind the Binding source to data
For each classInstance as myClass in MyCollection
MyBindingSource.List.Add(classInstance)
Next
A user may add or delete items to/from this List. My aim is to save this updated List to the database. I need to determine if my binding source's list has changed (i.e. has items added to it, or has items removed from it).
I am aware that I could implement the INotifyPropertyChanged on my custom class, and exploit the OnPropertyChanged event, but my classes' property would never change in my case. The other solution that I can think of is to use the BindingSource's ListChanged event, and maintain a collection of all the added and the deleted rows there. Although this approach may work for me, I reckon its a bit flaky.
Does the binding source or a Collection (such as the IList in my case) provide any other properties that can help me determine the above?

Binding to Dictionary ValueCollection

I have a Dictionary whose ValueCollection consists of classes, which is exposed as a property in my VM, and bound to a DataGrid on the UI. My problem is that the Dictionary is populated asynchronously (it's actually an ObservableDictionary, so I can monitor change events), and no results ever show up in the grid. I'm raising the PropertyChanged event in the VM for the property the DataGrid is bound to whenever an item is added to the Dictionary, but can't get any results. I have verified that the events do fire.
If I use a straight ObservableCollection instead of an ObservableDictionary, everything is fine, and I don't need to raise PropertyChanged even, but I really wanted to use Dictionary to hold my collection to keep a unique key on each entry.
Am I doing something wrong, or is there a workaround with a different collection that I could use? I've also tried calling .ToObservable() or .ToEnumerable() on the value collection for binding.
The problem is that your ObservableDictionary probably implements INotifyCOllectionChanged (good), but your ObservableDictionary.Values does not.
Override the non generic IEnumerable.GetEnumerator method on your dictionary to return just the values instead of KeyValuePairs and bind the dictionary to your grid.
The problem was solved by returning Dictionary.Values.ToList() in my VM instead of just .Values, .Values.GetEnumerator(), or .Values.ToObservable(). I did have to still raise PropertyChanged in my VM for the VM property returning the list.

Silverlight: Listbox bind to CollectionViewSource\List update on remove of item

I have a Listbox that I binds to a resource (sort) CollectionViewSource in my XAML. Then in my cs code I set the CollectionViewSource source to List of objects (class level field)
I then have "remove button" that checks the selected items in the Listbox and removes them from the List of objects (class level field).
I thought the Listbox should update automatically since the items source updated.
Am I missing a step or property setting ?
Or am I missing something about how binding works?
tep
The class that contains your list of objects must implement INotifyPropertyChanged and you must raise the notification event when the list changes, passing in the name of the property that changed. This is what notifies the UI that it must update anything bound to that property.
Alternatively, make your collection of objects an ObservableCollection<T> and that will do the notifying for you.

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