如何从menitem中查找选定的项目
我有一个 ContextMenu,其中我在运行时将一些项目绑定为 MenuItem。 我还订阅了 Click
事件。
现在如何从事件中的绑定项目中找到点击了哪个项目?
谢谢,
I have a ContextMenu
in which I bound some items as MenuItem
at runtime.
I have subscribed a Click
event also.
Now how to find wwhich item was clicked from the bound item in the event?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这只是猜测,但在 WPF 中,主要有两种方式。第一个是检查对象的
SelectedItem
属性(如果有)。另一种是检查传递给事件的参数。如果您的事件有一个 RoutedEventArgs,则此变量将包含一个名为 Source 的属性,该属性应包含调用该事件的对象。例如,当您在
ListView
ItemTemplate
中有一个Button
并且您想知道单击了哪一个时,就可以使用此方法。This is merely speculation, but in WPF, there are two main ways. First one is to check the
SelectedItem
property from your object if it has one.The other one is to chech the arguments that are passed to the event. If your event have a
RoutedEventArgs
, this variable will contains a property calledSource
which should contains the object that called the event. This could be used for example, when you have aButton
inside aListView
ItemTemplate
and you want to know which one was clicked.