silverjam
silverjam

Reputation: 13

Dynamic child user controls MVVM

I have a user control on a page and I'd like to load another custom user control within it's grid. However I want the user control type to be dynamic - e.g selecting "Calculator" from a drop down list would display my custom calculator control and selecting "Currency Converter" would load my currency converter. Can anybody help?

By the way I am trying to stick to MVVM.

Thanks in advance.

Upvotes: 1

Views: 380

Answers (2)

YeahStu
YeahStu

Reputation: 4052

John Papa has written a great blog post on exactly how to do this here.

The basic strategy is to instantiate a new instance of a class that derives from UserControl once selected from the drop down.

In other words, the SelectionChanged event of the drop down list could be handled and a new UserControl object created based on the selected item.

Upvotes: -1

anivas
anivas

Reputation: 6557

One way is to define multiple DataTemplates one for each child. As user selects the option set the corresponding ViewModel to a ContentControl's DataContext present in parent view and framework will pick the corresponding view for you.

Upvotes: 3

Related Questions