Mices7
Mices7

Reputation: 11

How to retrieve the name of a control created programmatically

in an app where it is possible to create a serie of buttons programmatically, how can I retrieve the name of one of these buttons once I click its context menu? This is the piece of code:

    private void addButton_Click(object sender, System.EventArgs e)
    {
       int y = (buttonIndex * 80) + 5;
       btn.Name = "btn" + buttonIndex.ToString();
       btn.Content = "button " + buttonIndex.ToString();
       btn.Width = 440;
       btn.Height = 100;
       Thickness margin = new Thickness(0, y, 0, 0);
       btn.Margin = margin;
       // .. all other properties..

       pivot1Grid.Children.Add(btn);
       buttonIndex++;
       AddContextMenuItems(btn);  
    }  

    private void AddContextMenuWithMenuItems(Button btn)
    {
       ContextMenu contextMenu = new ContextMenu();
       MenuItem menuItem1 = new MenuItem() { Header = "Edit", Tag = "Edit" };
       MenuItem menuItem2 = new MenuItem() { Header = "Remove", Tag = "Remove" };
       menuItem1.Click += new RoutedEventHandler(menuItem1_Click);
       menuItem2.Click += new RoutedEventHandler(menuItem2_Click);
       contextMenu.Items.Add(menuItem1);
       contextMenu.Items.Add(menuItem2);
       ContextMenuService.SetContextMenu(btn, contextMenu);
    } 

Now in the click event I should be able to get the name of the button clicked (it works in case the context menu is created directly in XAML):

    void menuItem1_Click(object sender, RoutedEventArgs e)
    { 
       string btnName = ((sender as MenuItem).Parent as ContextMenu).Name;
       ..

    }

but it gives always an empty string. Could you please tell me where is the mistake? Thanks

Upvotes: 1

Views: 145

Answers (1)

Kevin Gosse
Kevin Gosse

Reputation: 39007

In your code, you're casting sender.Parent to ContextMenu. So you know you are manipulating the ContextMenu object. Then why are you expecting to magically get the name of the button by querying the name of the context menu?

The easiest way in your case is to store your information in the Tag property of the context menu:

private void AddContextMenuWithMenuItems(Button btn)
{
   ContextMenu contextMenu = new ContextMenu();
   MenuItem menuItem1 = new MenuItem() { Header = "Edit", Tag = "Edit" };
   MenuItem menuItem2 = new MenuItem() { Header = "Remove", Tag = "Remove" };
   menuItem1.Click += new RoutedEventHandler(menuItem1_Click);
   menuItem2.Click += new RoutedEventHandler(menuItem2_Click);
   contextMenu.Items.Add(menuItem1);
   contextMenu.Items.Add(menuItem2);

   // Store the name of the button in the Tag property of the context menu
   contextMenu.Tag = btn.Name;

   ContextMenuService.SetContextMenu(btn, contextMenu);
} 

Then, in the event handler, you just have to retrieve the value you've set before:

void menuItem1_Click(object sender, RoutedEventArgs e)
{ 
   var contextMenu = (ContextMenu)((MenuItem)sender).Parent;
   string btnName = (string)contextMenu.Tag;
   ..
}

Upvotes: 1

Related Questions