Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Right now I'm working migrating an app I made entirely using code behind to MVVM and had a question on where I'm supposed to be creating controls dynamically.

Basically I have a web service that returns {#} of items. For each item a button will be created and the item will be assigned to its data context.

Now so far I know that I should set the Command to the ViewModel as well as the Command property. Also I know I should call those items in my web service inside of the view model (or model, right now its irrelevant to the question). The part that is questionable is where to create the buttons.

I didn't really like the idea of creating buttons in my ViewModel since it was, well, related to the View. Is this correct or should I be creating them inside the ViewModel and then some how pass them back to the View via Messaging?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

If the items are presented via an ItemsControl, ListBox, etc. then you can define an ItemTemplate and in that template, have a Button which binds appropriately. When Silverlight/WPF sees the object type, it'll "inflate" to what the ItemTemplate defines.

A good resource.

share|improve this answer
add comment

Your viewmodel should expose the colleciton of items, and the view should consume those items through databinding, creating the buttons in whatever control (list etc) is consuming them.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.