[英]How to add event on the context menu for chart in c#
I have a chart in WPF and when I right click on the chart it goes to the function Chart_mouseRightButtonDown which is described below.I want to add a context menu.I have already added two options to the context menu.but now I want some action when a user click ont hose options.How to add a handler to it ? 我在WPF中有一个图表,当我右键单击该图表时,会转到下面描述的功能Chart_mouseRightButtonDown。我想添加一个上下文菜单。我已经在上下文菜单中添加了两个选项。但是现在我想要一些操作当用户单击软管选项时,如何向其中添加处理程序?
private void Chart_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
var originalSource = e.OriginalSource as DependencyObject;
if (originalSource is Ellipse)
{
ContextMenu contextMenu = new ContextMenu();
contextMenu.Items.Add("Lookup Details in Grid");
contextMenu.Items.Add("Lookup Defect Info");
runTrendChart.ContextMenu = contextMenu;
}
else
runTrendChart.ContextMenu = null;
}
Instead of adding just strings to ContextMenu
Items
collection you should add a new MenuItem element: 不应将字符串仅添加到
ContextMenu
Items
集合中,而应添加一个新的MenuItem元素:
contextMenu.Items.Add(MenuItem()
{
Header = "Lookup Details in Grid",
Command = ANY_COMMAND_YOU_WANT
});
I don't see the reason why you are adding this MenuItems in MouseRightButtonDown
handler instead of XAML, but this should solve your problem. 我没有看到为什么要在
MouseRightButtonDown
处理程序而不是XAML中添加此MenuItems的原因,但这应该可以解决您的问题。
Edit If you don't want to use a Command
the MenuItem
has a Click
event to which you can add an event handler. 编辑如果不想使用
Command
则MenuItem
具有一个Click
事件,可以向其添加事件处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.