tags:

views:

132

answers:

3

Having tried a number of different solutions I keep coming back to this. I need a Window.ShowDialog, using the ViewModelLocator class as a factory via a UnityContainer.

Basically I have a View(and ViewModel) which on a button press on the the view needs to create a dialog (taking a couple of parameters in its constructor) that will process some logic and eventally return a result to the caller (along with the results of all the logic it computed).

Maybe I'm wrong for stilll looking at this from a Windows Forms perspective, but I know exactly what I want to do and I want to ideally do it using WPF and MVVM. I'm trying to make this work for a project, and ultimately don't want to have to go back to vanilla WPF in order to make it work.

A: 

You can do that. Just create an instance of a page/usercontrol/window and do a instance.ShowDialog()

Here's my T4 templates to generate a view/viewmodel with the messaging for closing a window and other tricks. http://bit.ly/auMxfx

Rick Ratayczak
A: 

I break the rules to implement a dialogwindow but tried to reduce it to a minimum. I have a method OpenDialog in my BaseViewModel:

public void OpenDialog(DialogViewModel model)
{
    this.MessengerInstance.Send<DialogViewModel, MainWindow>(model);
}

And in my MainWindow:

Messenger.Default.Register<DialogViewModel>(this, model =>
        {
            // Instantiate the dialog box
            var dlg = new DialogWindow();
            // Configure the dialog box
            dlg.Owner = this;
            dlg.Content = model;
            // Open the dialog box modally 
            dlg.ShowDialog();
        });

That way i only have a loose coupling between my viewmodel and my MainView. You can do the same for closing, my BaseDialogViewModel has a method:

public void CloseDialog()
    {
        this.MessengerInstance.Send<PopUpAction, DialogWindow>(PopUpAction.Close);
    }

(PopupAction is just an enum) and my DialogWindow registers for that:

Messenger.Default.Register<PopUpAction>(this, action =>
        {
            switch (action)
            {
                case PopUpAction.Close:
                    this.Close();
                    break;
            }
        });

You could also leave the receiver away when sending, to keep the view class out of the viewmodel but either way i think it's a acceptable solution :)

CodeWeasel
A: 

Any guidance on where to place this code?

SiKo