Silverlight Toolkit ContextMenu:右键单击了哪个控件?
Silverlight Toolkit 有一个可爱的 ContextMenu,它可以在多个控件实例(例如 Textbox)之间共享。共享可以通过在还托管其他控件的容器中声明 ContextMenu 来实现。
<StackPanel>
<TextBox x:Name="box1" Text="{Binding str1}" />
<TextBox x:Name="box2" Text="{Binding str2}" />
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Name="cm">
<toolkit:MenuItem Name="cmiCut" Header="Cut" />
<toolkit:MenuItem Name="cmiCopy" Header="Copy" />
<toolkit:Separator/>
<toolkit:MenuItem Name="cmiPaste" Header="Paste" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</StackPanel>
还可以通过调用 ContextMenuService.SetContextMenu 来实现共享。
当ContextMenu被共享时,事件处理程序知道右键单击哪个控件打开ContextMenu(例如上下文)非常有帮助。谁能提供一种有效的方法来做到这一点?
为了进行比较,这种需求在其他平台中得到解决,如下所示:
- WPF 的 ContextMenu 有 ContextMenu.PlacementTarget
- WinForms 的 ContextMenuStrip 有 ToolStripItem.Owner.SourceControl
谢谢,
Bill
The Silverlight Toolkit has a lovely ContextMenu, which can be shared among multiple instances of controls such as Textbox. Sharing can result from the ContextMenu being declared in a container which also hosts other controls.
<StackPanel>
<TextBox x:Name="box1" Text="{Binding str1}" />
<TextBox x:Name="box2" Text="{Binding str2}" />
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Name="cm">
<toolkit:MenuItem Name="cmiCut" Header="Cut" />
<toolkit:MenuItem Name="cmiCopy" Header="Copy" />
<toolkit:Separator/>
<toolkit:MenuItem Name="cmiPaste" Header="Paste" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</StackPanel>
Sharing can also be achieved with a call to ContextMenuService.SetContextMenu.
When the ContextMenu is shared, it's very helpful for the eventhandler to know which control was right-clicked to open the ContextMenu (e.g. context). Could anyone offer an efficient way to do this?
For comparison, this need is addressed in other platforms as follows:
- WPF's ContextMenu has ContextMenu.PlacementTarget
- WinForms' ContextMenuStrip has ToolStripItem.Owner.SourceControl
Thanks,
Bill
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我要感谢 Erik Noren 的博客关于这个话题。我在 MainPage.xaml 上的 Visibility=Collapsed 矩形中定义了 ContextMenu,这样它就不会处理鼠标右键单击事件。当在页面上的任意位置单击鼠标右键时,我会
在单击位置识别一个文本框,然后打开上下文菜单。 Erik 查找具有 SelectedText 依赖属性的控件的技术非常出色。
I'd like to thank Erik Noren for blogging on this topic. I defined my ContextMenu in a Rectangle with Visibility=Collapsed on my MainPage.xaml so that it doesn't handle the mouse right click event. When the right mouse button is clicked anywhere on the page, I use
to identify a Textbox at the click position and then open the ContextMenu. Erik's technique for finding a control with SelectedText dependency property is brilliant.