title: append ContextMenu to Silverlight Image Control in Runtime
body: <p>I'm trying to do these in runtime in my silverlight application:</p>
  
  <p>1- create a list of images
  2- define a custom ContextMenu for any of them
  My code in as Below:</p>
  
  <pre><code>void AddImageViewerToGridCells(Grid g)
      {
          for (int i = 1; i &lt;= g.RowDefinitions.Count; i++)
              for (int j = 1; j &lt;= g.ColumnDefinitions.Count; j++)
              {
                  var b = new Image();
                  b.Source = new BitmapImage(new Uri(@"Drop.jpg", UriKind.Relative));
                  b.SetValue(Image.NameProperty, "img" + i.ToString() + "_" + j.ToString());                    
                  b.Stretch = Stretch.Fill;
                  b.SetValue(Grid.RowProperty, i-1);
                  b.SetValue(Grid.ColumnProperty, j-1);
                  b.MouseRightButtonDown += b_MouseRightButtonDown;
                  g.Children.Add(b);
              }
      }
  
  void b_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
      {
          Image img = (Image)sender;
          ContextMenu cm = new ContextMenu();
          MenuItem mi1 = new MenuItem();
          mi1.Header = "Add To List";
          mi1.Click += mi1_Click;
          cm.Items.Add(mi1);
          MenuItem mi2 = new MenuItem();
          mi2.Header = "Remove From List";
          cm.Items.Add(mi2);
          ContextMenuService.SetContextMenu(img, cm);
      }
  </code></pre>
  
  <p>Bu I can't see my ContextMenu.
  As I know I should deliver a dependencyObject To SetContextMenu Method, But (just as I guess)  my runtime control is not a dependency Object.</p>
  
  <p>Thanks for any help</p>