質問

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?
}
役に立ちましたか?

解決

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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top