title: Reusing a ContextMenu for multiple controls in WPF
body: <p>I have three tree view controls which house different (but mostly similar data), as a result the actions that can be taken at each level is the same as far as the user is concerned, but different in their type (which is something I have to worry about as the developer). What I would <em>like</em> to do is reuse this context menu and pass in a type to it (or be able to retrieve it) and then the type would get passed through to the actual Executed function.</p>
  
  <p>So that we can have a common verbage here's some code:</p>
  
  <pre><code>&lt;UserControl x:Class="ucControl" 
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local="clr-namespace:LocalNamespace"
   Name="ucControl" 
   xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
   Background="LightGray"&gt;
   &lt;UserControl.Resources&gt;
       &lt;ContextMenu x:Key="GroupMenu"&gt;
           &lt;MenuItem Header="Add Group" Command="New"/&gt;
           &lt;MenuItem Header="Rename Group" Command="local:CustomCommands.RenameGroup"/&gt;
           &lt;MenuItem Header="Delete Group" Command="Delete"/&gt;
           &lt;Separator/&gt;
           &lt;MenuItem Header="Change Contents of Group" Command="local:CustomCommands.EditGroupContents"/&gt;
       &lt;/ContextMenu&gt;
   &lt;/UserControl.Resources&gt; 
   &lt;UserControl.CommandBindings&gt;
       &lt;CommandBinding CanExecute="CanAddGroup" Command="New" Executed="AddGroup"/&gt;
       &lt;CommandBinding Command="local:CustomCommands.RenameGroup" CanExecute="CanRename" Executed="RenameGroup"/&gt;
       &lt;CommandBinding Command="local:CustomCommands.EditGroupContents" CanExecute="CanEditGroupContents" Executed="EditGroupContents"/&gt;
   &lt;/UserControl.CommandBindings&gt;
   &lt;TabControl Name="tcTabs"&gt;
       &lt;TabItem Header="Size" Name="tiSize"&gt;
           &lt;TreeView Name="tvSizeGroup" ContextMenu="{StaticResource GroupMenu}"/&gt;
       &lt;/TabItem&gt;
       &lt;TabItem Header="Brand" Name="tiBrand"&gt;
           &lt;TreeView Name="tvBrandGroup" ContextMenu="{StaticResource GroupMenu}"/&gt;
       &lt;/TabItem&gt;
       &lt;TabItem Header="Color" Name="tiColor"&gt;
           &lt;TreeView Name="tvColorGroup" ContextMenu="{StaticResource GroupMenu}"/&gt;
       &lt;/TabItem&gt;
   &lt;/TabControl&gt;
  </code></pre>
  
  <p></p>
  
  <p>In this example, I would like for each of Size, Brand, and Color to have the same ContextMenu, but if the Context Menu is pulled up in the Size TreeView, I will need to add a group of type Size to my table. Is this possible the way I am doing it? I am trying to avoid making three ContextMenus, which would obviously be a way to do this.</p>