简体   繁体   English

从上下文菜单中获取所选菜单项的名称(或索引),该菜单项是通过绑定到ObservableCollection的ItemsSource动态生成的

[英]Get name(or index) of selected menu item from context menu, which was dynamically generated via ItemsSource bound to a ObservableCollection

I have a context menu that contains 1 menu item. 我有一个包含1个菜单项的上下文菜单。 That menu item is bound to a ObservableCollection for the itemssource. 该菜单项已绑定到itemssource的ObservableCollection。

         <ListView.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Example Menu Item" 
                          Command="{Binding Path=DataContext.ExampleCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}"
                          ItemsSource="{Binding ObservableItems}">
                </MenuItem>
            </ContextMenu>
        </ListView.ContextMenu>

How do I get the name (or index) of the menu item that was selected. 如何获得所选菜单项的名称(或索引)。 The problem is I cannot bind a command to each individual menu item, as they are dynamically generated. 问题是我无法将命令绑定到每个单独的菜单项,因为它们是动态生成的。

For example how would I know which item was clicked, as seen in the image below? 例如,我如何知道单击了哪个项目,如下图所示?

在此处输入图片说明

Any help is much appreciated. 任何帮助深表感谢。 Thanks. 谢谢。

You still can bind Command and CommandParameter per item for dynamically generated lists but you need to use ItemContainerStyle 您仍然可以将每个项目的CommandCommandParameter绑定为动态生成的列表,但是您需要使用ItemContainerStyle

<ContextMenu>
    <MenuItem Header="Example Menu Item" ItemsSource="{Binding ObservableItems}">
        <MenuItem.ItemContainerStyle>
            <Style TargetType="{x:Type MenuItem}">
                <Setter Property="Command" Value="{Binding Path=DataContext.ExampleCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}"/>
                <Setter Property="CommandParameter" Value="{Binding}"/>
            </Style>
        </MenuItem.ItemContainerStyle>
    </MenuItem>
</ContextMenu>

in this example CommandParameter , which is passed to you ExampleCommand command as parameter, will be an item in your collection (current DataContext of child item) 在本示例中, CommandParameter作为参数传递给您的ExampleCommand命令将成为您集合中的一个项目(子项目的当前DataContext

EDIT 编辑

To get index you can use pair of ItemsControl properties: AlternationCount and AlternationIndex . 要获得索引,您可以使用对ItemsControl属性: AlternationCountAlternationIndex You set AlternationCount to number of items in your collection and pass AlternationIndex to your command 您将AlternationCount设置为集合中的项目数,并将AlternationIndex传递给命令

<MenuItem Header="Example Menu Item" ItemsSource="{Binding ObservableItems}" AlternationCount="{Binding ObservableItems.Count}">
   <MenuItem.ItemContainerStyle>
      <Style TargetType="{x:Type MenuItem}">
         <Setter Property="Command" Value="{Binding ...}"/>
         <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}"/>
      </Style>
   </MenuItem.ItemContainerStyle>
</MenuItem>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM