Mehrshad
Mehrshad

Reputation: 55

Set a ViewModel's property, By opening another View(binded to another ViewModel) from the Main View

I'm a beginner in WPF and MVVM but I'm trying to do my best in implementing the MVVM pattern in my application (Basically I am a Java Developer switched to C# recently). My problem in MVVM is this scenario:

There are two models like this:

class MyObj
{
    public string Name {get; set;}
    public MyOtherObj OtherObj {get; set;}
    //and some other properties
}

class MyOtherObj
{
    public int ID{get; set;}
    //and some other properties
}

and two view models: MyObjVM which has a MyObj object and MyOtherObjVM which has a MyOtherObj object. And a view MyObjView which is bind to MyObjVM and has a button. By clicking on this button another View(MyOtherObjView) with MyOtherObjVM as it's DataContext should be opened and by closing the second view the MyOtherObj object which is created in MyOtherObjVM should be passed to MyObj object of the MyObjVM in the first view.

I googled it but found nothing except some frameworks like MVVMLight, but I didn't find good documentation or case studies. Is there any possible way of solving this problem without using a third-party framework, or using frameworks is the wise choice? (I mean does it need a lot of code or it's simple to implement because I prefer to learn rather than using frameworks, but my time is limited).

And at last sorry for my bad English :D

Upvotes: 0

Views: 557

Answers (2)

Simon Guindon
Simon Guindon

Reputation: 21

Inter-ViewModel communication is easily handled by MVVMLight's Messenger or Caliburn.Micro's EventAggregator. I preferred Caliburn.Micro's implementation since it was interface based rather than subscribing directly to message types. This allows for far more flexibility with inheritance of base classes.

Both of these act like a central event bus that various parts subscribe to. This really helps you send messages and handle them across boundaries.

Great thing is you don't need the whole MVVMLight or Caliburn.Micro libraries to implement this. You can just pluck the EventAggregator into your project. You'll find EventAggregator very useful for many other scenarios as well to enable messaging of decoupled parts.

Another great thing to do is create reusable behaviors that send messages over the EventAggregator. Both of these features together can enable some really good Blendable scenarios.

Example of MVVMLight Messenger: http://geekswithblogs.net/lbugnion/archive/2009/09/27/mvvm-light-toolkit-messenger-v2-beta.aspx

Example of Caliburn.Micro EventAggregator: http://www.codeconfessions.com/2011/03/from-prism-to-caliburn-micro-event-aggregation/

Upvotes: 1

Johnny
Johnny

Reputation: 11

It seems the same as How to Open a New Window from VM. There are some solutions.

Use Message,In the MVVMLight, They call it mediator, there is a Messenger in the framework, if you want to open a new window, you can register the message from the View part, while in the VM , you just send the message out.

Upvotes: 0

Related Questions