Solved

Do I need to raise CollectionChanged?

Posted on 2010-11-30
6
1,735 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

705 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