简体   繁体   English

如何从上下文菜单中获取所选项目

[英]How to get the selected item from a context menu

I have a context menu which is binded to a list of items.我有一个绑定到项目列表的上下文菜单。 When I click on an item, the command executes however I am having problems passing the selected item as a parameter, can anyone identify what I'm doing wrong?当我单击一个项目时,该命令会执行,但是我在将所选项目作为参数传递时遇到了问题,谁能确定我做错了什么?

I have tried passing {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}} as the command parameter however it just gives me the whole list within the context menu and when.我曾尝试将{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}作为命令参数传递{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}但是它只是给了我上下文菜单中的整个列表和时间。 When I tried {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}} no value was seen.当我尝试{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}}没有看到任何值。

    <ContextMenu x:Key="SelectFileTab" ItemsSource="{Binding ContextMenuItems}" x:Name="contextmenu">
        <ContextMenu.ItemTemplate>
            <DataTemplate>
                <MenuItem Header="{Binding}">
                </MenuItem>
            </DataTemplate>
        </ContextMenu.ItemTemplate>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="PreviewMouseUp">
                <i:InvokeCommandAction Command="{Binding SelectedFileToAdd, Mode=OneWay}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>

    </ContextMenu>     

I really over complicated something quite simple, there was no need to use interaction.triggers issue to the command, all I needed to do was我真的把一些很简单的事情复杂化了,没有必要在命令中使用interaction.triggers 问题,我需要做的就是

<ContextMenu x:Key="SelectFileTab" ItemsSource="{Binding ContextMenuItems}" x:Name="contextmenu">
        <ContextMenu.ItemTemplate>
            <DataTemplate>
                 <MenuItem Header="{Binding}" Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.SelectedFileToAdd}"
                                       CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
            </DataTemplate>
        </ContextMenu.ItemTemplate>
   </ContextMenu>

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

相关问题 如何从列表框到上下文菜单中获取选定的项目 - How to get selected Item from listbox to context menu 如何为上下文菜单单击事件获取选定的列表视图项 - How to get selected list view item for context menu click event 如何通过打开上下文操作菜单从ListView中获取所选项目? (Xamarin.Forms) - How do I get the selected item from a ListView by opening the context actions menu? (Xamarin.Forms) 无法获得所选项目的上下文菜单 - Can't get selected item context menu 从上下文菜单中获取所选菜单项的名称(或索引),该菜单项是通过绑定到ObservableCollection的ItemsSource动态生成的 - Get name(or index) of selected menu item from context menu, which was dynamically generated via ItemsSource bound to a ObservableCollection 如何从附加到按钮的菜单中获取所选项目 - How do I get the selected item from Menu attached to a button 如何从Glass收到有关已选择自定义菜单项的通知? - How to get a notification from Glass that a custom menu item was selected? 使用上下文菜单后获取选定的 ListView 项目 - Get selected ListView item after using Context Menu 右键单击上下文菜单后,获取选定的项目 - Get Item selected after right click on context menu 如何通过上下文菜单中的复制命令复制选定的列表视图项目 - How to copy selected listview item by copy command in the context menu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM