如何在单击按钮时在WPF表单上创建菜单? [英] How can I create a menu on WPF form on button click?

查看:80
本文介绍了如何在单击按钮时在WPF表单上创建菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我到目前为止所做的。



(发件人为Button).ContextMenu.IsEnabled = true;

(发件人为Button).ContextMenu.PlacementTarget =(发送者为Button);

(发件人为Button).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;

(发件人为Button).ContextMenu.IsOpen = true;







但我得到了一个例外说对象引用没有设置为对象的实例。

我是编程新手所以我对此错误一无所知,也不知道如何解决它所以请帮助我。

This is what i did so far.

(sender as Button).ContextMenu.IsEnabled = true;
(sender as Button).ContextMenu.PlacementTarget = (sender as Button);
(sender as Button).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
(sender as Button).ContextMenu.IsOpen = true;



but i got an exception saying "Object reference not set to an instance of an object".
I am new to programming so i don't know anything about this error nor do i know anything about how to resolve it so please help me.

推荐答案





例外是由于以下错误之一:

1.发件人不是按钮

2.按钮没有上下文菜单。



如果案例是第一个,我在没有更多信息的情况下帮助你,但如果是第二个,请执行此操作:

1.声明av可以存储你的按钮:

按钮btn =发送者为按钮

2.在按钮上创建上下文菜单:

btn.ContextMenu = new ContextMenu

3.继续进行,就像你到目前为止一样。



这是因为Button的ContextMenu属性实际上不存在,这是因为你产生了这个错误...这对于新手很常见......所以第二步,你可以创建一个与...一起工作...


顺便说一句
,您可以使用菜单元素来创建菜单...在msdn上查看!



希望这会有所帮助...



LG
Hi,

the exception is due to one of these errors:
1. Sender is not a button
2. The button does not have a contextmenu.

If the case is the first one, I cannpt help you without more information, but if it's the second, do this:
1. Declare a variable to store your button like that:
Button btn = sender as Button
2. Create the context menu on the button like that:
btn.ContextMenu=new ContextMenu
3. Proceed like you have done so far.

This is becuase the ContextMenu property of the Button does actually not exists and this is because you gen that error... it is very common for newbies... So with the second step, you can create one to work with...

by the way, you can use the menu element to create a menu... check it out on msdn!

Hope this helps...

LG


这篇关于如何在单击按钮时在WPF表单上创建菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆