Solved

Do I need to raise CollectionChanged?

Posted on 2010-11-30
6
1,601 Views
Last Modified: 2012-08-14
I have an ObservableCollection that is set in Sub New via a service call.

Public Property MyColl as ObservableCollection(of MyClass)

Public Sub New()
.....
....
MyColl = ServiceCall.Result
End Sub

I see the collection in my UI.

During processing, the service call may be repeated.

Public Sub SomeGoodReason
...
MyColl = ServiceCall.Result
End Sub

I know the collection has actually changed. Shouldn't the setting of the collection property to the result of the service call cause the Collection Changed event to fire because it's an ObservableCollection?

or does there have to be an explicit MyColl.Add (or Remove or Clear, etc)?

And if I have to fire the event myself, can you give me a VB snippet?

Thanks.
0
Comment
Question by:AIBMass
  • 3
  • 3
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34238815
No it won't fire because the collection itself hasn't changed. The thing that has changed is that the MyColl variable now points to a different collection.

You can't raise the event yourself because it is a Protected method of the class. You'll either need to add/remove a dummy item, or implement INotifyPropertyChanged and raise your own event.
0
 

Author Comment

by:AIBMass
ID: 34238936
I'm sure you are correct. However, I try:

... implements INotifyCollectionChanged

Public Event CollectionChanged (sender as Object, e as NotifyCollectionChangedEventArgs) implements....

Then:

Me.MyColl = ServiceCall.Result
RaiseEvent CollectionChanged (Me.MyColl, _
    new(NotifyCOllectionChangedEventArgs(NotifyCollectionChangedAction.Reset))


and I still don't see my changed.

Did I set the event arguments correctly?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34239011
Implementing INotifyCollectionChanged probably isn't going to work if your class doesn't derive from Collection.

Instead of simply replacing your collection, it might be better to do (untested so might need tweaking):
MyColl.Clear()
MyColl.AddRange(ServiceCall.Result)

Open in new window

That way you are actually modifying the current collection, rather than replacing it, which should make the normal event mechanism play properly.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:AIBMass
ID: 34239115
Hmmm...MyColl does derive from Collection, it's an ObservableCollection - yes?

And AddRange does not exist for an ObservableCollection.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 34239201
MyColl may be a collection, but the class that contains it isn't; and that is the one on which you are trying to implement the interface.

Try clearing the collection, then looping through teh new collection and adding the items to MyColl. That way you'll be able to see if it is firing properly.
0
 

Author Closing Comment

by:AIBMass
ID: 34239269
Ultimately I had to use:

Me.MyColl.Clear
For each it as MyClass in ServiceCall.Result
  Me.MyColl.Add(it)
Next
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now