1

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.

Community
  • 1
  • 1
vikox
  • 347
  • 1
  • 3
  • 7

3 Answers3

0

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

Alex Shtoff
  • 2,520
  • 1
  • 25
  • 53
  • No, if you read carefoully, i wrote, that when i am assigning newErrorViewModel, i do not see view, only text written in the window. – vikox Dec 20 '10 at 20:48
  • So you are actually saying the your DialogWindow doesn't instantiate the correct DataTemplate for its Content? If its content is of yype NewErrorViewModel it must use the DataTemplate you provided and instantiate NewErrorView. – Alex Shtoff Dec 23 '10 at 11:36
0

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.

vikox
  • 347
  • 1
  • 3
  • 7
0

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).

jbe
  • 6,976
  • 1
  • 43
  • 34