5

Possible Duplicate:
The best approach to create new window in WPF using MVVM

Hello Friends,

I have two view MainWindowView and AddCustomerView. I have menu containing buttons in MainwindowView.xmal.

How could i popup AddCustomerView from MainWindowViewModel by clicking on button.

My App.xmal.cs for Startup code is..

base.OnStartup(e);
MainWindow window = new MainWindow();
var viewModel = new MainWindowViewModel();
window.DataContext = viewModel;
window.Show();

What is the code for showing AddCustomerView in buttonexecute code.

 public void AddNewCustomerWindowExecute() //This is button handler
 {
     // How to show AddCustomerView from MainWindowViewModel
 }
Community
  • 1
  • 1
KillerFish
  • 5,042
  • 16
  • 55
  • 64

2 Answers2

23

Handle it in the view

Probably the most simple approach.

private void AddCustomerView_Click(object sender, RoutedEventArgs e)
{
    AddCustomerView view = new AddCustomerView(data);
    view.Show();
}

ViewModel exposes an event

This has one drawback: it requires lots of manual coding.

public class MainWindowViewModel 
{
    public event EventHandler AddCustomerViewShowed;

    public void AddNewCustomerWindowExecute()
    {
        if (AddCustomerViewShowed != null)
            AddCustomerViewShowed(this, EventArgs.Empty);
    }
}

Handle it in the view

var viewModel = new MainWindowViewModel();
viewModel.AddCustomerViewShowed += (s, e) => new AddCustomerView(data).Show();

Controller that handles all your views

public class Controller : IController
{
    public void AddCustomer()
    {
        AddCustomerView view = new AddCustomerView(data);
        view.Show();
    }
}

public class MainWindowViewModel 
{
    IController controler;

    public MainWindowViewModel(IController controller)
    {
        this.controller = controller;
    }

    public void AddNewCustomerWindowExecute()
    {
        controller.AddCustomer();
    }
}

Mediator pattern

Some MVVM frameworks (e.g. MVVM Light) use this pattern.

public class App // or in the view or somewhere else
{
    public void RegisterMessenger()
    {
        Messenger.Default.Register<AddCustomerMessage>(this, ProcessAddCustomerMessage);            
    }

    private void ProcessAddCustomerMessage(AddCustomerMessage message)
    {
        AddCustomerView view = new AddCustomerView(data);
        view.Show();
    }
}

public class MainWindowViewModel 
{
    public void AddNewCustomerWindowExecute()
    {
        Messenger.Default.Send(new AddCustomerMessage(...));
    }
}
mak
  • 13,267
  • 5
  • 41
  • 47
  • Thank You but i am new to WPF - MVVM, I am not understanding this complex code. – KillerFish Apr 29 '11 at 08:22
  • @Killerfish I presented to you 4 different approaches. Pick one that you like the best – mak Apr 29 '11 at 08:26
  • that solution looks good. I used it - works but I am not sure if it is a good solution, since I had to add another non static constructor to App.cs. Could you give me a hint where should I register correctly that message which is responsible for showing View? App is a good solution? Or maybe Locator, but locator? I will be grateful for advise. – komizo Aug 29 '14 at 08:49
  • This is a great answer, but you use the variable `data` a lot, and I don't see it defined or assigned anywhere. Where is that coming from? – rory.ap Dec 08 '16 at 12:48
3

Check out this "deep dive MVVM video". Laurent Bugnion shows the Concept of the IDialogService and explains the concepts very well... plus the source code should also be available... The concepts apply also to wpf

http://channel9.msdn.com/Events/MIX/MIX11/OPN03

HTH

silverfighter
  • 6,762
  • 10
  • 46
  • 73
  • For some reason the Channel 9 video doesn't play. However, if you click below the video display area and select the "High Quality MP4" hyperlink, it will play. All of the other links will give you a 404 not found error. – John C Mar 03 '14 at 06:48