Question

I have encountered some problems in the bookmark section for my wpf web-browser. I want to be able to delete already existing buttons but I can't seem to figure out how to detect the content of the button I right click on. (To get my ContextMenu to show).

My visual progress so far: http://puu.sh/6Dxat.png

Adding a context menu to the buttons:

public void button_MouseRightButtonDown(object sender, MouseButtonEventArgs e)//add a context menu to buttons
{
    Button button = sender as Button;
    menu = new ContextMenu();
    menu.Items.Add(new MenuItem() { Header = "Delete" });
    button.ContextMenu = menu;
    menu.Closed += button_DeleteButtonClicked;//find the right event
 }

(I know that the event is wrong, but that is not important right now.)

And the event:

private void button_DeleteButtonClicked(object sender, RoutedEventArgs e)//delete bookmark
{
    //This is where I need help. I want the content (which is the URL) of the button 
    //right clicked onto, for example, show up in a messagebox. How to do?
}
Was it helpful?

Solution

Since you have hooked Close event of context menu here, so sender will be ContextMenu here and you can get button by using PlacementTarget property of ContextMenu.

private void button_DeleteButtonClicked(object sender, RoutedEventArgs e)
{
    Button button = ((ContextMenu)sender).PlacementTarget as Button;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top