Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从上下文菜单中获取所选菜单项的名称(或索引),上下文菜单是通过绑定到ObservableCollection的ItemsSource动态生成的_C#_Wpf_Mvvm_Contextmenu_Menuitem - Fatal编程技术网

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

C# 从上下文菜单中获取所选菜单项的名称(或索引),上下文菜单是通过绑定到ObservableCollection的ItemsSource动态生成的,c#,wpf,mvvm,contextmenu,menuitem,C#,Wpf,Mvvm,Contextmenu,Menuitem,我有一个包含1个菜单项的上下文菜单。该菜单项绑定到itemssource的ObservableCollection <ListView.ContextMenu> <ContextMenu> <MenuItem Header="Example Menu Item" Command="{Binding Path=DataContext.Exam

我有一个包含1个菜单项的上下文菜单。该菜单项绑定到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>

如何获取所选菜单项的名称(或索引)。问题是我无法将命令绑定到每个单独的菜单项,因为它们是动态生成的

例如,我如何知道单击了哪个项目,如下图所示


非常感谢您的帮助。谢谢。

对于动态生成的列表,您仍然可以为每个项目绑定
命令
命令参数
,但您需要使用
项目容器样式

<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>

对于动态生成的列表,您仍然可以为每个项绑定
命令
命令参数
,但需要使用
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>

是否有可能获取选定的索引,而不是observablecollection项?如果您有item和collection从那里获取索引(它位于同一个
DataContext
),我不确定您的意思是什么?你是说在集合中进行迭代吗?它应该和使用像
var idx=observeItems.IndexOf(selectedItem)
这样的方法一样简单。检查我的编辑,你应该得到一个项目的索引而不是项目。是否可以得到所选的索引,而不是observablecollection项目?如果您有项目和集合从那里获取索引(它位于相同的
DataContext
),我不太确定您的意思是什么?你是说在集合中进行迭代吗?这应该和使用类似
var idx=observeItems.IndexOf(selectedItem)
的方法一样简单,检查我的编辑,你应该得到一个项的索引而不是项的索引