Programatically adding,changing and removing controls bound to model objects (MVVM)

Posted by Ittai on Stack Overflow See other posts from Stack Overflow or by Ittai
Published on 2010-12-26T17:08:53Z Indexed on 2010/12/26 21:54 UTC
Read the original article Hit count: 197

Filed under:
|
|
|
|

I have the following scenario and I'm trying to decide how it can and should be implemented in silverlight and c# via MVVM.

Scenario:
I have the following objects in my scenario: MyModel, MyModelControl, MyView and MyViewModel.

MyModelcontrol is a custom UserControl of mine and the rest I think are pretty much self explanatory.

MyViewModel is a Subscriber of a message which hands it a Collection<MyModel> newCol and let's mark curCol as the current Collection<MyModel> that MyViewModel holds.

I then want to programatically add MyModelControl controls for each MyModel instance which belongs to newCol and not to curCol.

I also want to bind the MyModelControl instance to the corresponding MyModel instance.

In a similar manner I want to remove the controls which don't belong now to the collection and I want to update a property for those which belong to both.

I do not want to remove all controls which belong to curCol and then add all controls for newCol as the creation of MyModelControl is "expensive".

I'd appreciate help with how this should be implemented in SL as I'm new to it and to the MVVM pattern.

Comments regarding the design would also be welcomed.

B.T.W This will run on Windows Phone 7 so I think there are some limitations on versions, if this is an issue please comment and I'll verify which versions exactly I can use on the phone.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET