简体   繁体   English

如何将所选上下文菜单项的索引传递给绑定命令?

[英]How to pass an index of a selected context menu item to a bound command?

How could i bind all context menu items to one single command, which gets the index of the menu item called? 我如何将所有上下文菜单项绑定到一个命令,该命令将获得菜单项的索引? I have a WPF/MVVM project. 我有一个WPF / MVVM项目。

<ContextMenu x:Key="cm" ItemsSource="{Binding ActionItems}" 
DisplayMemberPath="ActionDescription">    
</ContextMenu>

Your should pass it as a CommandParameter : 您应该将其作为CommandParameter传递:

<ContextMenu x:Key="cm" ItemsSource="{Binding ActionItems}" DisplayMemberPath="ActionDescription"
Command="{Binding YourCommand}" CommandParameter="{Binding YourParameter}">
...
</ContextMenu>  

Updated you need the solution described here: MVVM binding command to contextmenu item 更新后,您需要这里描述的解决方案: MVVM绑定到contextmenu项的命令

Updated after comments 评论后更新

relapse - than you should implement it as shown here: WPF ContextMenu with ItemsSource - how to bind to Command in each item? 复发-比您应该按以下所示实现它: 带ItemsSource的WPF ContextMenu-如何在每个项目中绑定到Command? . Please notice that link is a duplication of other question. 请注意,该链接是其他问题的重复。 So read the both please. 所以请阅读两者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 树视图上下文菜单-将所选项目传递给命令? - Tree View Context Menu - Pass selected item to command? 从上下文菜单中获取所选菜单项的名称(或索引),该菜单项是通过绑定到ObservableCollection的ItemsSource动态生成的 - Get name(or index) of selected menu item from context menu, which was dynamically generated via ItemsSource bound to a ObservableCollection 如何通过上下文菜单中的复制命令复制选定的列表视图项目 - How to copy selected listview item by copy command in the context menu 如何从上下文菜单中获取所选项目 - How to get the selected item from a context menu 如何从列表框到上下文菜单中获取选定的项目 - How to get selected Item from listbox to context menu 如何为上下文菜单单击事件获取选定的列表视图项 - How to get selected list view item for context menu click event 与使用绑定的ViewModel对象(MVVM)相比,将所选项目作为命令参数传递 - Pass selected item as command parameter vs. using a bound ViewModel Object (MVVM) 如何获取触发上下文菜单项的索引 - how do I get the index of a firing context menu item 如果禁用命令,如何更改上下文菜单项的样式 - How to change context menu item's style if command is disabled WPF中DiagramSurface上的Selected项的上下文菜单 - context menu for Selected item on a DiagramSurface in WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM