how to load wpf usercontrol in MVVM pattern

前端 未结 6 1825
刺人心
刺人心 2021-02-06 12:36

I\'m creating a wpf user control which is in mvvm pattern. So we have : view(with no code in codebehind file), viewmodel,model,dataaccess files.

I have MainWindo

6条回答
  •  情书的邮戳
    2021-02-06 13:05

    I've been using MVVM Light Toolkit which has a ViewModelLocator class that you can put properties to the viewmodels in. You then create a reference to the ViewModelLocator in your Mainwindow.xaml like so:

    
    

    In the grid panel, or whatever you're using, you can then set the datacontext like this:

    
    ...
    
    

    You could also go with MEFedMVVM which potentially adds a bit more flexibility in terms of being able to swap different viewModel implementations into the view.

    The flexibility in both of these libraries is that you don't have to use their ViewModel base classes if you don't want to - the ViewModelLocator and the MEFedMVVM can work with any class.

提交回复
热议问题