How to initialise my WPF app using MVVMLight?
I am building a WPF application using MVVM Light and having problems tying up my Views to my View Model. I have a view model in which I am passing in an Interface of IDataContext, which is basically passing in a datacontext.Then I have an View 开发者_高级运维which I have inserted the following statement on top
DataContext="{Binding MyViewModel, Source={StaticResource Locator}}"
In my ViewModelLocator I have added the following lines :
IoC.Register<IDataContext, MyDataContext>();
and
IoC.Register<MyViewModel>();
And after putting a break point in the constructor of the 'MyViewModel', the breakpoint gets hit. Is there something I am missing, please help???
Did you define a public getter for your viewModel in ViewModelLocator class as shown below?
public MyViewModel MainViewModel
{
get
{
return SimpleIoc.Default.GetInstance<MyViewModel>();
}
}
and then use the "MainViewModel" in the view binding as shown below
DataContext="{Binding MainViewModel, Source={StaticResource Locator}}"
What kind of behavior you see? Empty view window with no datacontext set? OR exception when displaying your view?
You can have a look at this http://nileshgule.blogspot.com/2011/05/integrate-mvvmlight-toolkit-with-basic.html
精彩评论