title: Caliburn.Micro: How to bind a function to a context menu item in the RowDetailsTemplate of a DataGrid?
body: <p>I have a datagrid of Items that includes a datagrid of SubItems for the row details. There is a context menu for the main "Items" datagrid with functions to add or remove items, this is working fine. There is also a context menu for the row details datagrid "SubItems" with similar functions that I cannot get to bind properly. If executed i get the following exception "System.Exception: 'No target found for method AddSubItem.'"</p>
  
  <p>I have desperately tried many other solutions that were suggested on other posts but nothing has worked so far. I appreciate any insight. Below is my test code.</p>
  
  <p>ShellViewModel:</p>
  
  <pre><code>using System;
  using Caliburn.Micro;
  using ContextMenuTest.Models;
  
  namespace ContextMenuTest.ViewModels
  {
      public class ShellViewModel : Screen
      {
          public static Random rnd = new Random();
          private BindableCollection&lt;ItemModel&gt; items = new BindableCollection&lt;ItemModel&gt;();
          public BindableCollection&lt;ItemModel&gt; Items
          {
              get { return items; }
              set
              {
                  items = value;
                  NotifyOfPropertyChange(() =&gt; Items);
              }
          }
  
          private ItemModel selectedItem = new ItemModel(rnd);
          public ItemModel SelectedItem
          {
              get { return selectedItem; }
              set
              {
                  selectedItem = value;
                  NotifyOfPropertyChange(() =&gt; SelectedItem);
              }
          }
  
          private SubItemModel selectedSubItem = new SubItemModel(rnd);
          public SubItemModel SelectedSubItem
          {
              get { return selectedSubItem; }
              set
              {
                  selectedSubItem = value;
                  NotifyOfPropertyChange(() =&gt; SelectedSubItem);
              }
          }
  
          public ShellViewModel()
          {
              for(int i = 0; i &lt; 10; i++)
              {
                  Items.Add(new ItemModel(rnd));
              }
          }
  
          public void AddItem()
          {
              Items.Add(new ItemModel(rnd));
          }
  
          public void RemoveItem()
          {
              Items.Remove(SelectedItem);
          }
  
          public void AddSubItem(object e)
          {
              var _item = e as ItemModel;
              _item.SubItems.Add(new SubItemModel(rnd));
          }
  
          public void RemoveSubItem(object e)
          {
              var _item = e as ItemModel;
              _item.SubItems.Remove(SelectedSubItem);
          }
      }
  }
  </code></pre>
  
  <p>ShellView:</p>
  
  <pre><code>&lt;Window x:Class="ContextMenuTest.Views.ShellView"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:ContextMenuTest.Views"
          xmlns:cal="http://www.caliburnproject.org"
          mc:Ignorable="d"
          Title="ShellView" Height="450" Width="800"
          &gt;
  
      &lt;Grid Grid.Row="1"&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="*"/&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;DataGrid x:Name="Items" Grid.Row="0" AutoGenerateColumns="False" CanUserResizeColumns="True" CanUserAddRows="false"
                          VerticalAlignment="Stretch" VerticalScrollBarVisibility="Auto" MinHeight="100" SelectedItem="{Binding SelectedItem}"&gt;
              &lt;DataGrid.ContextMenu&gt;
                  &lt;ContextMenu cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"&gt;
                      &lt;MenuItem Header="Add Item" cal:Message.Attach="AddItem()"/&gt;
                      &lt;MenuItem Header="Remove Item" cal:Message.Attach="RemoveItem()"/&gt;
                  &lt;/ContextMenu&gt;
              &lt;/DataGrid.ContextMenu&gt;
  
              &lt;DataGrid.Columns &gt;
                  &lt;DataGridTextColumn Header="ID" Binding="{Binding Path=ID, UpdateSourceTrigger=PropertyChanged}" Width="*" IsReadOnly="True"/&gt;
                  &lt;DataGridTextColumn Header="Name" Binding="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" Width="*"/&gt;
              &lt;/DataGrid.Columns&gt;
  
              &lt;DataGrid.RowDetailsTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;DataGrid ItemsSource="{Binding Path=SubItems}"  AutoGenerateColumns="False" CanUserResizeColumns="True" CanUserAddRows="false"
                                  VerticalAlignment="Stretch" VerticalScrollBarVisibility="Auto" MinHeight="100" 
                                    SelectedItem="{Binding Path=SelectedSubItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"&gt;
                          &lt;DataGrid.ContextMenu&gt;
                              &lt;ContextMenu cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"&gt;
                                  &lt;MenuItem Header="Add Sub Item" cal:Message.Attach="AddSubItem($dataContext)"/&gt;
                                  &lt;MenuItem Header="Remove Sub Item" cal:Message.Attach="RemoveSubItem($dataContext)"/&gt;
                              &lt;/ContextMenu&gt;
                          &lt;/DataGrid.ContextMenu&gt;
                          &lt;DataGrid.Columns&gt;
                              &lt;DataGridTextColumn Header="ID" Binding="{Binding Path=ID}" Width="auto" IsReadOnly="True"/&gt;
                              &lt;DataGridTextColumn Header="Name" Binding="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" Width="auto"/&gt;
                              &lt;DataGridTextColumn Header="Value" Binding="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}" Width="auto"/&gt;
                          &lt;/DataGrid.Columns&gt;
                      &lt;/DataGrid&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGrid.RowDetailsTemplate&gt;
          &lt;/DataGrid&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>ItemModel:</p>
  
  <pre><code>using Caliburn.Micro;
  using System;
  
  namespace ContextMenuTest.Models
  {
      public class ItemModel
      {
          public Guid ID { get; set; }
          public string Name { get; set; }
          public BindableCollection&lt;SubItemModel&gt; SubItems { get; set; } = new BindableCollection&lt;SubItemModel&gt;();
  
          public ItemModel(Random rnd)
          {
              ID = Guid.NewGuid();
              Name = "Item " + ID.ToString().Substring(3, 5);
  
              for (int i = 0; i &lt; 5; i++)
              {
                  SubItems.Add(new SubItemModel(rnd));
              }
          }
      }
  }
  </code></pre>
  
  <p>SubItemModel:</p>
  
  <pre><code>using System;
  
  namespace ContextMenuTest.Models
  {
      public class SubItemModel
      {
          public Guid ID { get; set; }
          public string Name { get; set; }
          public int Value { get; set; }
  
          public SubItemModel(Random rnd)
          {
              ID = Guid.NewGuid();
              Name = "SubItem " + ID.ToString().Substring(3,5);
              Value = rnd.Next();
          }
      }
  }
  </code></pre>