ContextMenu调用者 [英] ContextMenu caller

查看:78
本文介绍了ContextMenu调用者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


ContextMenu中的MenuItem如何实现称为ContextMenu的对象?我有数据绑定的treeView,我想知道哪个TextBlock项是右键单击的.我不想在后面使用databinded对象,我需要使用TextBlock.谢谢!

解决方案

通过 PlacementTarget [ ^ ]属性

嗨 我认为这段代码可以帮助您

<pre language="c#"><br />
void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)<br />
{<br />
     TreeNode myNode =  treeView1.GetNodeAt(e.X, e.Y);<br />
     if(myNode != null)<br />
          //your code like treeView1.SelectedNode = myNode;<br />
     else<br />
          //your code like MessageBox.Show("No treenode selected", "Invalid selection");<br />
}</pre>


请记住,如果if块是必需的,则else编译器可能会给出NullRefrenceException
希望对您有帮助


Hi,
how can a MenuItem in ContextMenu realize what object called the ContextMenu? I have databinded treeView and I want to know, which TextBlock item was right-clicked. I don''t want the databinded object behind, I need work with the TextBlock. Thanks!

解决方案

Take a look through this[^] from MSDN. It is about a ContextMenu on a TreeView but I think the techniques (and there are a few of them) should work for you.

You might also take a look at the PlacementTarget[^] property of the ContextMenu.


Hi I think this code can help u

<pre language="c#"><br />
void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)<br />
{<br />
     TreeNode myNode =  treeView1.GetNodeAt(e.X, e.Y);<br />
     if(myNode != null)<br />
          //your code like treeView1.SelectedNode = myNode;<br />
     else<br />
          //your code like MessageBox.Show("No treenode selected", "Invalid selection");<br />
}</pre>


Just remember that the if block is necessary else compiler may give a NullRefrenceException
Hope this help you


这篇关于ContextMenu调用者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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