我在wpf网络浏览器的书签部分遇到了一些问题。我希望能够删除现有的按钮,但我似乎无法弄清楚如何检测按钮的内容我右键单击。 (要让ContextMenu
显示)。
My visual progress so far: http://puu.sh/6Dxat.png
向按钮添加上下文菜单:
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
}
(我知道事件是错误的,但现在这并不重要。)
事件:
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?
}
答案 0 :(得分:1)
由于您已在此处挂接关闭上下文菜单的事件,因此发件人将在此ContextMenu
,您可以使用ContextMenu的PlacementTarget属性获取按钮。
private void button_DeleteButtonClicked(object sender, RoutedEventArgs e)
{
Button button = ((ContextMenu)sender).PlacementTarget as Button;
}