Reputation: 445
Well I saw many solutions on this type of the problem but I just can't solve my problem. I work on some wpf/mvvm application. My app is structured like this:
So the problem is this: I need to assign new datacontext to my window, from my viewmodel class. Something like this (in my view model class):
SomeWindow window = new SomeWindow();
window.DataContex = this.someViewModel;
Problem is i can't access my View classes. I added reference of my viewmodel into my view project, but can't add reference of my view into my viewmodel becouse I get the above error. How can I solve this??
Upvotes: 1
Views: 4532
Reputation: 22435
a viewmodel has no need to "know"/ reference the view. so you dont need a reference in your viewmodel project.
if you wanna change a datacontext for a view - you should simply change the "workspace" property of the viewmodel which is bind to that view.
Upvotes: 1
Reputation: 1688
I cannot understand why it's necessary to assign data context in viewModels project (why not in Views project). But if you need to that this way, the most common approach is to create another (middle) project for those purposes. This new project will have references to the Views project and ViewModels project.
From the other hand ViewModels and Views are often in the same project (just in other folders/namespaces), so in this case that problem doesn't exists.
Upvotes: 3