title: Memory leak in WPF app due to DelegateCommand
body: <p>I just finished desktop apps written in WPF and c# using MVVM pattern. In this app I used Delegate Command implementation to wrap the ICommands properties exposed in my ModelView. The problem is these DelegateCommands prevent my ModelView and View from being garbage collected after closing the view. So it stays larking until I terminate the whole application. I profile the application I find it’s all about delegatecommand that keeping the modelview in memory. 
  How could I avoid this situation and is this in nature of mvvm pattern, or it’s about my implantation of the pattern?. Thanks.</p>
  
  <p>Edit: this is small but complete portion of how i implement MVVM pattern</p>
  
  <p>First: CommandDelegte class</p>
  
  <pre><code>class DelegateCommand:ICommand
  {
      private Action&lt;object&gt; execute;
      private Predicate&lt;object&gt; canExcute;
      public DelegateCommand(Action&lt;object&gt; execute, Predicate&lt;object&gt; canExecute)
      {
          if (execute == null)
          {
              throw new ArgumentNullException("execute");
          }
          this.execute = execute;
          this.canExcute = canExecute;
      }
      public bool CanExecute(object parameter)
      {
          if (this.canExcute != null)
          {
              return canExcute(parameter);
          }
          return true;
      }
  
      public event EventHandler CanExecuteChanged
      {
          add { CommandManager.RequerySuggested += value; }
          remove { CommandManager.RequerySuggested -= value; }
      }
  
  
      public void Execute(object parameter)
      {
          this.execute(parameter);
      }
  }
  </code></pre>
  
  <p>Second: ModelView Class</p>
  
  <pre><code>public class ViewModel:DependencyObject, INotifyPropertyChanged
  {
      private DelegateCommand printCommand;
  
      public ICommand PrintCommand
      {
          get
          {
              if (printCommand == null)
              {
                  printCommand = new DelegateCommand(Print, CanExecutePrint);
              }
              return printCommand;
          }
      }
      void Print(object obj)
      {
          Console.WriteLine("Print Command");
  
      }
      bool CanExecutePrint(object obj)
      {
          return true;
      }
  
  
      public event PropertyChangedEventHandler PropertyChanged;
      private void OnProeprtyChanged(string propertyName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>
  
  <p>Third: Window code behind </p>
  
  <pre><code>public MainWindow()
      {
          InitializeComponent();
          base.DataContext = new ViewModel();
      }
  </code></pre>
  
  <p>Forth: My XAML</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Window.InputBindings&gt;
      &lt;KeyBinding Key="P" Modifiers="Control" Command="{Binding Path=PrintCommand}"/&gt;
  &lt;/Window.InputBindings&gt;
  &lt;StackPanel&gt;
      &lt;Button Content="Print - Ctrl+P" Width="75" Height="75" Command="{Binding Path=PrintCommand}"/&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p></p>