开发者

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 开发者_JAVA技巧I am trying to stick to MVVM.

Thanks in advance.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜