title: Context Menu for user control in wpf
body: <p>I'm developing a WPF application using MVVM pattern. I use  user controls.
  I want to do some view /copy &amp; Paste via Context menu in the user control.</p>
  
  <p>I was able to add user context menu in list view/ Grid view etc. I was able to do with styles.</p>
  
  <p>I wish to add some context menu directly to user control. I tried the following code, it does not work :(</p>
  
  <p>Can any one help?</p>
  
  <pre><code>UserControl x:Class="MYOWN.ScenarioView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:SMTF" 
      mc:Ignorable="d" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="392" d:DesignWidth="484"  &gt;
  
  &lt;UserControl.Resources&gt;
      &lt;ResourceDictionary&gt;
          &lt;ResourceDictionary.MergedDictionaries&gt;
              &lt;ResourceDictionary Source="../../DefaultStyle.xaml" /&gt;
          &lt;/ResourceDictionary.MergedDictionaries&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/UserControl.Resources&gt;
  
  &lt;UserControl.ContextMenu&gt;
      &lt;ContextMenu Visibility="Visible"&gt;
          &lt;MenuItem Header="Standard MenuItem 2" /&gt;
          &lt;MenuItem Header="Standard MenuItem 3" /&gt;
      &lt;/ContextMenu&gt;
  &lt;/UserControl.ContextMenu&gt;
  &lt;Grid Height="394" Width="486" &gt;
  
      &lt;Label Content="Category" Height="28" HorizontalAlignment="Left" Margin="13,36,0,0" Name="lblCategory" VerticalAlignment="Top" /&gt;
      &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="118,106,0,0" Name="txtScenario" VerticalAlignment="Top" Width="343" Text="{Binding Scenario_Desc}" /&gt;
      &lt;Button Content="Save" Command="{Binding SaveData}" Height="23" HorizontalAlignment="Left" Margin="294,348,0,0" Name="btnSave" VerticalAlignment="Top" Width="75" /&gt;
      &lt;Button Content="Reset" Command="{Binding ClearData}" Height="23" HorizontalAlignment="Left" Margin="386,348,0,0" Name="btnReset" VerticalAlignment="Top" Width="75" /&gt;
      &lt;Label Content="Sub Category" Height="28" HorizontalAlignment="Left" Margin="13,70,0,0" Name="lblSubCategory" VerticalAlignment="Top" Grid.RowSpan="2" Grid.ColumnSpan="2" /&gt;
      &lt;ComboBox Height="23" HorizontalAlignment="Left" Margin="118,36,0,0" Name="cboCategory" VerticalAlignment="Top" Width="343" 
               ItemsSource="{Binding Path=Category}"
               DisplayMemberPath="Category_Desc"
               SelectedValuePath="Category_Id"
               SelectedValue="{Binding Path=Category_Id}"
               SelectedIndex="0"
               Text="{Binding Category_Desc}"&gt;
          &lt;i:Interaction.Triggers&gt;
              &lt;i:EventTrigger EventName="SelectionChanged"&gt;
                  &lt;i:InvokeCommandAction Command="{Binding CategorySelected}"
                                     CommandParameter="{Binding SelectedValue, ElementName=cboCategory}"/&gt;
              &lt;/i:EventTrigger&gt;
          &lt;/i:Interaction.Triggers&gt;
      &lt;/ComboBox&gt;
  
  
  
      &lt;Label Content="Scenario" Height="28" HorizontalAlignment="Left" Margin="15,104,0,0" Name="lblScenario" VerticalAlignment="Top" /&gt;
      &lt;ComboBox  Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="343" ItemsSource="{Binding Path=SubCategory}" Margin="118,70,0,0" Name="cboSubCategory" 
                 DisplayMemberPath="Sub_Category_Desc"
                 SelectedValue="{Binding Path=Sub_Category_Id}" 
                 SelectedValuePath="Sub_Category_Id" 
                 Text="{Binding Sub_Category_Desc}"  /&gt;
  
      &lt;GroupBox Header=" Script " Height="200" HorizontalAlignment="Left" Margin="13,135,0,0" Name="grpScript" VerticalAlignment="Top" Width="458"&gt;
          &lt;Grid&gt;
              &lt;TextBlock HorizontalAlignment="Left" Margin="30,00,0,100" Height="26"&gt;
              &lt;ToggleButton  x:Name="Add" Focusable="False" Command ="{Binding ShowNew}" Style="{StaticResource SMToggle}" &gt;
                  &lt;Image Source="/Image/Add.png"  Width="16" Height="16" /&gt;
              &lt;/ToggleButton&gt;
             &lt;ToggleButton  x:Name="Select" Focusable="False" Command ="{Binding ShowSelect}" Style="{StaticResource SMToggle}" &gt;
                  &lt;Image Source="/Image/select.png"  Width="16" Height="16" /&gt;
              &lt;/ToggleButton&gt;
  
              &lt;/TextBlock&gt;
  
              &lt;ListView  AlternationCount="2"  Height="160" Width="343" HorizontalAlignment="Left" Margin="97,10,0,0" Name="lvTestCase" VerticalAlignment="Top" 
                         ItemContainerStyle="{StaticResource ListViewItemStyle}" ItemsSource="{Binding Script}" &gt;
                  &lt;i:Interaction.Triggers&gt;
                      &lt;i:EventTrigger EventName="MouseDoubleClick"&gt;
                          &lt;i:InvokeCommandAction Command="{Binding EditData}"/&gt;
                      &lt;/i:EventTrigger&gt;
                  &lt;/i:Interaction.Triggers&gt;
                  &lt;ListView.View&gt;
                      &lt;GridView x:Name="grdScript"&gt;
                          &lt;GridViewColumn DisplayMemberBinding="{Binding Path=Script_Text}" Header="Script" Width="315" /&gt;
                      &lt;/GridView&gt;
                  &lt;/ListView.View&gt;
  
              &lt;/ListView&gt;
          &lt;/Grid&gt;
      &lt;/GroupBox&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>