vikox
vikox

Reputation: 347

Modal Window with MVVM pattern

I am trying to make modal dialog window for let user know some error messages, or let user edit some values. I am using MVVM pattern, so my mainwindow has some control part and workspace part. In workspace part, i am opening viewmodels tight with datatemplate to views (defined as usercontrols). From one of these views i want to open modal dialog window. I was following this answer Error window show modal in MVVM WPF. As described in that answer, i have implemented the DialogClass in an InvoiceViewModel. But i have problem with showing the content of the modal window. If I set the content of the window to ViewModel class, output is simple text with namespace path to that ViewModel. (ViewModel attached to the View with datatemplate.) If I set content to the View - it is working - view is showed but, i am disobeing MVVM pattern (opening View from ViewModel as ViewModel has no reference to View).

        ErrorViewModel newErrorViewModel = new ErrorViewModel();
        ErrorView newErrorView = new ErrorView();
        DialogWindow dialogWindow = new DialogWindow();
        //Not Working
        //dialogWindow.Content = newErrorViewModel;

        //Working But, breaking MVVM 
        dialogWindow.Content = newErrorView;
        dialogWindow.ShowDialog();

In ErrorView.xaml i have attached ErrorViewModel like this.

<DataTemplate DataType="{x:Type vm:ErrorViewModel}">
    <vw:ErrorView/>
</DataTemplate>

What am i doing wrong?

Thanks.

Upvotes: 1

Views: 2048

Answers (3)

jbe
jbe

Reputation: 7031

Maybe you are interested to see an alternative implementation which uses the Managed Extensibility Framework (MEF) for this scenario. Please have a look at the ViewModel sample application of the WPF Application Framework (WAF).

Upvotes: 0

vikox
vikox

Reputation: 347

Got it.

I have placed code for wiring up model and view in ErrorView.xaml. This is my mistake of course. I have replaced these lines into app.xaml resource and it is working lika a charm.

Upvotes: 0

Alex Shtoff
Alex Shtoff

Reputation: 2640

You are assigning newErrorView to the dialog's content, instead of newErrorViewModel

Upvotes: 0

Related Questions