Archive | WPF RSS for this section

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();
}

Read More…