You have to bind the Click
event of the MenuItem
. It's probably going to end up looking like this (I don't have the means of testing this code right now, but it should be close):
// (old) ...
menu.Items.Add(new MenuItem() { Header = "Delete" });
// ...
// (tweak to) ...
var deleteMenuItem = new MenuItem() { Header = "Delete" };
deleteMenuItem.Click += button_DeleteButtonClicked;
menu.Items.Add(deleteMenuItem);
// no need to bind menu.Close for this
// ...
Binding ContextMenu.Close
is useful if you want to perform some code no matter what the user chose, but it doesn't look like it's useful to you in this particular scenario.
The handler method should also be adjusted, of course, to accomodate the different sender
:
// ...
Button button = ((ContextMenu)((MenuItem)sender).Parent).PlacementTarget as Button;
// .SourceControl could also be a viable alternative to .PlacementTarget
// ...