How to programmatically create/populate view respecting MVVM?

In my WPF application I have a MainView composed of three Views (custom UserControls). The layout of my views are done (in XAML) but now I have to read a configuration file and populate my views. When I read the configuration file, the model objects should be created as well as the view items.

The more complex view (lets call it View1) will have tabs comprising a set of buttons separated by categories (arranged in StackPanels+GroupBox).

Since the main components of the view are buttons that will trigger actions of my model, my question is: how to achieve this using MVVM?

Will the ViewModel be responsible for creating it's view? If not, how would it use commands?

PS I'm using MVVM Light Toolkit


No, the ViewModel is not responsible for creating its view. Generally people use XAML DataTemplates to map a ViewModel object to a UserControl. This is known as "ViewModel first". You can also create the views yourself, and inject the ViewModels manually. This is known as "View first". Generally ViewModel first seems more popular.

I'm not sure what your issue is with the buttons. You simply expose RelayCommands in your ViewModel, and you bind your buttons to them in the view.

链接地址: http://www.djcxy.com/p/56176.html

上一篇: 正确创建MVVM WPF应用程序的方法

下一篇: 如何以编程方式创建/填充关于MVVM的视图?