title: Attached Behavior to execute command for ListViewItem
body: <p>I am trying to use an attached behavior to execute a command in my ViewModel when the user Double Clicks on the list item.</p>
  
  <p>I have reviewed a number of articles on the subject, and tried creating a simple test application but am still having problems eg.
  <a href="https://stackoverflow.com/questions/1035023/firing-a-double-click-event-from-a-wpf-listview-item-using-mvvm">Firing a double click event from a WPF ListView item using MVVM</a>  </p>
  
  <p>My simple test ViewModel has 2 collections, one that returns a list of strings and the other that returns a List of ListViewItem types</p>
  
  <pre><code>public class ViewModel
  {
      public ViewModel()
      {
          Stuff = new ObservableCollection&lt;ListViewItem&gt;
                      {
                          new ListViewItem { Content = "item 1" },
                          new ListViewItem { Content = "item 2" }
                      };
  
          StringStuff = new ObservableCollection&lt;string&gt; { "item 1", "item 2" };
      }
  
      public ObservableCollection&lt;ListViewItem&gt; Stuff { get; set; }
  
      public ObservableCollection&lt;string&gt; StringStuff { get; set; }
  
      public ICommand Foo
      {
          get
          {
              return new DelegateCommand(this.DoSomeAction);
          }
      }
  
      private void DoSomeAction()
      {
          MessageBox.Show("Command Triggered");
      }
  }
  </code></pre>
  
  <p>Here is the attached property which is like may other examples you see:</p>
  
  <pre><code>public class ClickBehavior
  {
      public static DependencyProperty DoubleClickCommandProperty = DependencyProperty.RegisterAttached("DoubleClick",
                 typeof(ICommand),
                 typeof(ClickBehavior),
                 new FrameworkPropertyMetadata(null, new PropertyChangedCallback(ClickBehavior.DoubleClickChanged)));
  
      public static void SetDoubleClick(DependencyObject target, ICommand value)
      {
          target.SetValue(ClickBehavior.DoubleClickCommandProperty, value);
      }
  
      public static ICommand GetDoubleClick(DependencyObject target)
      {
          return (ICommand)target.GetValue(DoubleClickCommandProperty);
      }
  
      private static void DoubleClickChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
      {
          ListViewItem element = target as ListViewItem;
          if (element != null)
          {
              if ((e.NewValue != null) &amp;&amp; (e.OldValue == null))
              {
                  element.MouseDoubleClick += element_MouseDoubleClick;
              }
              else if ((e.NewValue == null) &amp;&amp; (e.OldValue != null))
              {
                  element.MouseDoubleClick -= element_MouseDoubleClick;
              }
          }
      }
  
      static void element_MouseDoubleClick(object sender, MouseButtonEventArgs e)
      {
          UIElement element = (UIElement)sender;
          ICommand command = (ICommand)element.GetValue(ClickBehavior.DoubleClickCommandProperty);
          command.Execute(null);
      }
  }
  </code></pre>
  
  <p>In my main window, I have defined the style which sets the attached behaviour and binds to the Foo command</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;Style x:Key="listViewItemStyle" TargetType="{x:Type ListViewItem}"&gt;
          &lt;Setter Property="local:ClickBehavior.DoubleClick" Value="{Binding Foo}"/&gt;                 
      &lt;/Style&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>Works fine when ListViewItems are defined:</p>
  
  <pre><code>&lt;!-- Works --&gt;
  &lt;Label Grid.Row="2" Content="DoubleClick click behaviour:"/&gt;        
  &lt;ListView Grid.Row="2" Grid.Column="1" ItemContainerStyle="{StaticResource listViewItemStyle}"&gt;
      &lt;ListViewItem Content="Item 3" /&gt;
      &lt;ListViewItem Content="Item 4" /&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>This works too, when bound to the list of type ListViewItem:</p>
  
  <pre><code>&lt;!-- Works when items bound are of type ListViewItem --&gt;
  &lt;Label Grid.Row="3" Content="DoubleClick when bound to ListViewItem:"/&gt;        
    &lt;ListView Grid.Row="3" Grid.Column="1" ItemContainerStyle="{StaticResource listViewItemStyle}" ItemsSource="{Binding Stuff}"&gt;        
   &lt;/ListView&gt;
  </code></pre>
  
  <p>But this doesn't:</p>
  
  <pre><code>&lt;!-- Does not work when items bound are not ListViewItem --&gt;
  &lt;Label Grid.Row="4" Content="DoubleClick when bound to string list:"/&gt;
    &lt;ListView Grid.Row="4" Grid.Column="1" ItemContainerStyle="{StaticResource listViewItemStyle}" ItemsSource="{Binding StringStuff}"&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>In the output window you see the error, but finding it difficult to understand what is wrong.<br>
  <em>System.Windows.Data Error: 39 : BindingExpression path error: 'Foo' property not found on 'object' ''String' (HashCode=785742638)'. BindingExpression:Path=Foo; DataItem='String' (HashCode=785742638); target element is 'ListViewItem' (Name=''); target property is 'DoubleClick' (type 'ICommand')</em></p>
  
  <p>So my quesion is: How can you get the Command wired up correctly to each ListViewItem when you bind your ListView to a list of Model objects?</p>
  
  <p>Thanks.</p>