Can anyone give out the best practices for playing around with controls at runtime such as creating a new view,adding views inside a view,adding controls to containers using MVVM pattern without breaking mvvm pattern??
I am using MVVMlight toolkit..
please help me out in this regard..
Thanks in advance……
This post discusses the strategies for creating views (dialogs) from your view models.
Edit:
From your comment I take it that you got an user interface that has an add and delete button. The add button should add an item (type ?) to a
ItemsControl… hope that’s correct.So, how would I do this, well I would create a view model that has an
ObservableCollecion<ItemViewModel>. TheItemViewModleis the view mode that represents the item that should be added to theItemsControl(so in your case the view model backing your “rangeView”).Then I would add two commands that handle the addition and deletion of items. Both commands just add/remove
ItemViewModelsfrom your collection.To show the items in the view I would bind the
ItemControl.ItemsSourceproperty to the collection in your main view model (i.e. the one holding theItemViewModelinstances). The I would supply an ItemTemplate to render the items on the screen.Ok, here is an example of what I think you are trying to do (at least conceptionally). Complete Source Code here. In the example I used a ListBox as it allows me easily to determine which item is selected, this depends on your szenario. Also note that you have complete freedom to customize the Template, the ItemPanelTemplate, and DataTemplate to fit your needs. You can even use this approacht to create PanoramaPages in WP7!
2nd edit:
ItemsControldoes not have aSelectedItemproperty. To get to it you have to use a control inheriting fromSelector(e.g. a ListBox as I did) or you can use theSelectordirectly.ItemViewModel:
MainViewModel:
MainPage.xaml