+1  A: 

Kai to make the change notification propagate to your bindings you should be making use of a collection which implements change notificaiton, PointCollection does not do this. You could create your own collection however I'd recommend making use of ObservableCollection<T>.

In addition here is a similar SO post which also touches on a few other options for making the UI aware of your changes.

Aaron
Ok, I also tried this approach. Is it necessary to use a ValueConverter for my ObservableCollection<Point>? Because after changing PointCollection to ObservableCollection<Point> the application runs but nothing gets visible although there are the values in the collection.
Kai Krupka
If you are using the Points property you should not need to as it is a DP. Make sure there are no errors in the output window as the pathing of your binding may be off. If you are passing the ObservableCollection<Point> into the DataContext it will simply be Points="{Binding}"
Aaron
if I say Points={Binding Points} there is no problem in the output window. But when I say this.DataContext=Points; in the code-behind and Points="{Binding}" in the view I get an error.
Kai Krupka
System.Windows.Data Error: 1 : Cannot create default converter to perform 'one-way' conversions between types 'System.Collections.ObjectModel.ObservableCollection`1[System.Windows.Point]' and 'System.Windows.Media.PointCollection'. Consider using Converter property of Binding. BindingExpression:Path=; DataItem='ObservableCollection`1' (HashCode=45943265); target element is 'Polyline' (Name='pline'); target property is 'Points' (type 'PointCollection')
Kai Krupka
What about the INotifyCollectionChanged? Should I implement this one, too?
Kai Krupka
Add a ValueConverter and convert the ObservableCollection to a PointCollection. Or create a custom PointCollection which implements INotifyCollectionChanged. You can do the first one for testing, however the custom collection would be the right way to go IMHO.
Aaron
ok great, with ValueConverter and converting a ObservableCollection to a PointCollection works. Problem now is that I pass it to the DataContext but for using this in my MVVM pattern and therefore I can't do it that way, right? For that I should use a DP, but then a error occurs during registration of the DP because the propertyType isn't a ObservableCollection<Point>, it's a PointCollection. How can I solve this problem? Is there another solution than a DP?
Kai Krupka
Making use of the DataContext is aligned with the MVVM pattern. How you go about pushing that data to the DataContext may not be in line however. You keep making references to a DP, however you do not have to have a DP on your ViewModel. If you simply keep the ObservableCollection as is, use your converter to swap it to a PointCollection or create your own PointCollection which implements INotifyCollectionChanged, then set the DataContext in the View via constructor injection ala Unity you will be rightfully aligned with the MVVM pattern in its entirety.
Aaron
If you do not care about a pure MVVM approach, get a reference to your view, set the DataContext of your View using your ViewModel with the property containing the Points and all should be good.
Aaron
Now it works completely with Mvvm. I don't know why it works now but it does. Because I already used the Unity Framework and my codebehind has one additional property: [Dependency] public ViewModel VM { set { _viewmodel = value; this.DataContext = _viewmodel; } } THANKS for your help!!!
Kai Krupka
Hey Aaron,you talked about a custom PointCpollection which implements INotifyCollectionChange. There is one problem which I don't understand. How is it possible to implement a custom PointCollection if i can't inherit from PointCollection because it's a sealed class?
Kai Krupka
@Kai Wasn't aware it was sealed, checked out these links, Bea can explain it much better then I can, gives you a few options to attack the problem, one of which uses our previous converter, http://bea.stollnitz.com/blog/?p=35 http://bea.stollnitz.com/blog/?p=36 http://bea.stollnitz.com/blog/?p=37
Aaron
this solutions looks great, but there is again a drawback to this. The problem is that I work with Tasks. Therefore I have to synchronize the UI-thread with the background thread. Normally I do this with the Dispatcher (passed via constructor injection to the necessary classes). But in this case my only option is to get my CurrentDispatcher in my Converter class. This works until a "System.Reflection.TargetParameterCountException" occurs and I have no clue where the error lies.
Kai Krupka
Are you using the BackgroundWorker? It'll marshal back to the UI thread for you...and encapsulates the logic nicely. It might be worth revisiting your threading model as you want it performing the complex processing or in the case of a web services call or file I/O when you do not know how long it is going to take. It sounds like the background thread and the UI are tightly coupled.
Aaron
The BackgroundWorker Class no. Not yet. But I'm not sure if I understand what you mean. But I explain my setting:
Kai Krupka
Every time I get a new data package multiple polylines get updated. For every polylien I call a kind of drawing-method which handles my new data and push it to the observable collection. Every Draw-Method is a seperate Task. If I'm right I have to synchronize the changed collections with the dispatcher?! The problem ist now that I don't get the UI thread and the other thread synchronized
Kai Krupka
Use a background worker to spin that off...then you can report progress, etc...once complete draw right on the UI thread with your newly received data.
Aaron
After some time everythingworks. Now I use the converter by Bea and the dispatcher asyn. The Backgroundworker has pros and cons in my situation but my decision was the dispatcher. But I think the converter by Bea has one disadvantage. The dictionary grows and grows and consequently my memory and cpu usage. now assume every second 4 dictionary entries were made and hold. After one minute there are already 240 entries and for every entry there are 800 points in my case. Do you have an idea for that problem? I think the problem lies in the fact that I replace points in my observable collection.
Kai Krupka
But my observable collection shouldn't be longer than a predefined size and after that values are replaced from the beginning on.
Kai Krupka
Kai you could create your custom observable collection, then override the add method, when the limit is reached, you can start removing then replacing the items as you need...
Aaron