How to open a new window using MVVM Light Toolkit How to open a new window using MVVM Light Toolkit wpf wpf

How to open a new window using MVVM Light Toolkit


Passing a message from ViewModel1 to View1 means to use the messaging capabilities in the MVVM Light Toolkit.

For example, your ViewModel1 could have a command called ShowView2Command, then it would send a message to display the view.

public class ViewModel1 : ViewModelBase{    public RelayCommand ShowView2Command { private set; get; }    public ViewModel1() : base()    {        ShowView2Command = new RelayCommand(ShowView2CommandExecute);    }    public void ShowView2CommandExecute()    {        Messenger.Default.Send(new NotificationMessage("ShowView2"));    }}

View1 would register to receive messages in its code behind and display View2 when it receives the correct message.

public partial class View1 : UserControl{    public View1()    {        InitializeComponent();        Messenger.Default.Register<NotificationMessage>(this, NotificationMessageReceived);    }    private void NotificationMessageReceived(NotificationMessage msg)    {        if (msg.Notification == "ShowView2")        {            var view2 = new view2();            view2.Show();        }    }}


Why do you go this route? Its simple. If you replace your button with a toggleButton, or a hyperlink, or any other number of button-like controls, you don't need to update your "code behind" - its a basic principle of the MVVM pattern. In your new toggleButton (or whatever), you still end up binding to the same exact Command.

For example, I'm creating a project for a client who wants to have 2 UI's - one is going to be fundamentally different in every way, in terms of presentation. Horizontal tabs vs Vertical RadPanelBar (think Accordion) for navigation. Both of these views can point to the same viewModel - when a user clicks the Work Order tab in View 1, it fires the same "WorkOrderCommand" that's fired in the Work Order Header in the panel bar.

In a code-behind model, you'd have to code two separate events. Here you only have to code one.

Furthermore, it allows a designer using Blend to create any layout they want. As long as they have the hooks (EventToCommand control) in place, myself (as a developer) couldn't care less what the final product looks like.

Loose coupling is incredibly powerful.


You can do in this way like you need to create some events and register those in view and call these in view model.and open that pop up window.

Like This example

public class Mainclass : MainView{    public delegate abc RegisterPopUp(abc A);    public RegisterPopUp POpUpEvent ;    public RelayCommand ShowCommand { private set; get; }      public void ShowCommand()     {         ShowCommand("Your parameter");    } }

inside the view MainView mn=new MainView();

Register the event here like thake mn.POpUpEvent += than click on tab button double time

and in registers popup method right the code for opening the pop up window.