[英]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.