Close a WPF View while abiding to the MVVM pattern

I want to start by saying that i am a total WPF using MVVM pattern newbie, but im getting the hang of it after working for a while with WPF and the MVVM pattern.

But i have to say its sometimes really hard to move eventhandlers from the views codebind to the viewmodel. Especialy when it commes to the Close method.My first thought was i could send the window as  an param to a command in the viewmodel but then that breaks the MVVM pattern and we cant be having that people.

Because you see, in the MVVM (Model – View – View Model) design pattern, the ViewModel (a class that typically contains the values you want to display in your View) shouldn’t know anything about the View.  However the view can and has to be aware of the ViewModel. But finally it came to me how i could implement the close eventhandler.

And i have to say i never thought it could be so hard to move an eventhandler like the one bellow from the views codebehind to the ViewModel:

private void  Close_Click(object sender, RoutedEventArgs e)
{
    this.Close();
}

Since the ViewModel isn’t supposed to know anything about the View in the MVVM design pattern. The Solution is to add an Action property to the ViewModel. In the ViewModel simply add :
public  Action CloseAction { get; set; }
And then we assign a delegate to the CloseAction Action property that we can simply invoke whenever we want to close the Window like so:
var esv = new ExportView();
var DataContext= new ExportViewModel(_dialogService);
if (DataContext.CloseAction == null)
  DataContext.CloseAction = new Action(() => esv.Close());
esv.DataContext = DataContext;
var msg = new SignalExportMessage() { ExportDataTable = ListToDataTable(_originalData) }; 
Messenger.Default.Send<SignalExportMessage>(msg);
esv.Show();
And now we are able to invoke the property from the Export viewmodel like so:
CloseAction();
If you search the web for ways to close a window in MVVM, you’ll find various methods using several lines of code, creating more classes. And all maner of other ways to do it.

But using the method described here, we can define the Close method with a single line on the ViewModel, two lines of code on the View/ViewModel, and invoke the Close method with a single line making a call to CloseAction(). So now you know of one more way how to close a window from the ViewModel instead of the view codebehind.

Advertisements

Tags: , ,

About ado_dado

I'm 32, work as an Systemdeveloper. Work mostly with .NET (C#) i also spend a lot of time with my best friend my lovely little pitbull/amstaff mix "Chili" :) and the rest is spent on several projects that i am involved in during my spare time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: