title: WPF Binding an ICommand to an event (FrameworkElement.Unloaded)
body: <p>I'm using the MVVM pattern so my view-model doesn't know anything about the view, and the view is displayed via DataTemplates.</p>
  
  <p>When the view isn't displayed anymore, I want to take a screenshot of it (with a utility class). So I want to bind to FrameworkElement.Unloaded, and when its hit, take a screenshot of the usercontrol for use in another control to select which view to go to.</p>
  
  <p>I read this article, which makes it appear as if attached properties would work (I'm using it on the UserControl object)
  <a href="http://blog.functionalfun.net/2008/09/hooking-up-commands-to-events-in-wpf.html" rel="nofollow">http://blog.functionalfun.net/2008/09/hooking-up-commands-to-events-in-wpf.html</a> </p>
  
  <p>I get the error that a binding can only be set on a DependencyObject or DependencyProperty. I followed his instructions properly. Any idea why this isnt working or how I can bind to that in a MVVM scenario?</p>
  
  <p>Is it not possible to bind to that particular event or to an event in the root xaml node?</p>
  
  <p>Here's teh code (in addition to the EventBehaviorFactory in the link above)</p>
  
  <pre><code>public static class FrameworkElementBehavior
  {
      public static readonly DependencyProperty UnloadedCommandProperty = EventBehaviourFactory.CreateCommandExecutionEventBehaviour(FrameworkElement.UnloadedEvent, "UnloadedCommand", typeof(FrameworkElementBehavior));
  
      public static void SetUnloadedCommand(DependencyObject o, ICommand value)
      {
          o.SetValue(UnloadedCommandProperty, value);
      }
  
      public static ICommand GetUnloadedCommand(DependencyObject o)
      {
          return o.GetValue(UnloadedCommandProperty) as ICommand;
      }
  }
  
  
      &lt;UserControl x:Class="WTF.BuxComp.Prism.Modules.Win.OrderEntryPos.Views.CustomerView"
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                   xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                   xmlns:local="clr-namespace:WTF.BuxComp.Prism.Modules.Win.OrderEntryPos.Helpers"
                   mc:Ignorable="d" 
                   d:DesignHeight="510" d:DesignWidth="716" 
  local:FrameworkElementBehavior.UnloadedCommand="{Binding UnloadedCommand}"&gt;
  </code></pre>
  
  <p>and the exact error is</p>
  
  <blockquote>
    <p>A 'Binding' cannot be set on the 'SetUnloadedCommand' property of type
    'CustomerView'. A 'Binding' can only be set on a DependencyProperty of
    a DependencyObject.</p>
  </blockquote>