title: C# mvvm how to handle window close events
body: <p>I am developing a wpf mvvm app with notes. Faced with the fact that I can not process the window closing event in the viewModel. I found similar questions, but the answers used Mvvm Light, which I would like to avoid. I can process it this way:</p>
  <p>FindNoteWindow.xaml</p>
  <pre><code>&lt;Window x:Class=&quot;NotesARK6.View.FindNoteWindow&quot;
      xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
      xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
      xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
      DataContext=&quot;{Binding Path=FindNoteWindowViewModel, Source={StaticResource ViewModelLocator}}&quot;
      mc:Ignorable=&quot;d&quot;
      Closing=&quot;Window_Closing&quot;
      Title=&quot;FindNoteWindow&quot; Height=&quot;250&quot; Width=&quot;400&quot;&gt;
  </code></pre>
  <p>FindNoteWindow.xaml.cs</p>
  <pre><code>public partial class FindNoteWindow : Window
  {
      public FindNoteWindow()
      {
          InitializeComponent();
      }
  
      private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
      {
          // code
      }
  }
  </code></pre>
  <p>But that's not what I need. I want to handle the close event in the viewModel like this:</p>
  <p>FindNoteWindow.xaml</p>
  <pre><code>&lt;Window x:Class=&quot;NotesARK6.View.FindNoteWindow&quot;
      xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
      xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
      xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
      DataContext=&quot;{Binding Path=FindNoteWindowViewModel, Source={StaticResource ViewModelLocator}}&quot;
      mc:Ignorable=&quot;d&quot;
      Closing=&quot;{Binding Window_Closing}&quot;
      Title=&quot;FindNoteWindow&quot; Height=&quot;250&quot; Width=&quot;400&quot;&gt;
  </code></pre>
  <p>FindNoteWindowViewModel.cs</p>
  <pre><code>    public void Window_Closing(object sender, CancelEventArgs e)
      {
          //code
      }
  </code></pre>
  <p>But if I do this, I get the error: InvalidCastException: Could not cast object type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'.
  Thanks in advance for your replies.</p>