Programatically adding,changing and removing controls bound to model objects (MVVM)
- by Ittai
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.