[英]How to get selected list view item for context menu click event
I need help to get Selected list view item details, when context menu assigned to list view items is clicked.当单击分配给列表视图项的上下文菜单时,我需要帮助来获取选定的列表视图项详细信息。
<ListView.Resources>
<ContextMenu x:Key="GvRowMenu" ItemsSource="{Binding ContextMenuItems}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding IconPath}"></Image>
<TextBlock Text="{Binding Name}"></TextBlock>
<MenuItem
Click="MenuItem_Click"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=DataContext.RunCommand}" />
This is a click event code这是点击事件代码
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
//what needs to de here?
}
I wrote this piece of code in my view model, but it doesnt trigger on execute method我在我的视图 model 中写了这段代码,但它不会在执行方法上触发
RunCommand = new DelegateCommand<object>(OnRunCommand, CanRunCommand);
private void OnRunCommand(object obj)
{
// use the object here...
}
private bool CanRunCommand(object obj)
{
return true;
}
Let me know, how can I handle this situation.让我知道,我该如何处理这种情况。 Any examples related to same will be appreciated.
任何与此相关的示例将不胜感激。
Thanks谢谢
you are mixing you methods... you can run an event or you can use a command, but not so much both together.你在混合你的方法......你可以运行一个事件或者你可以使用一个命令,但不能同时使用。
what you want is to bind the command:你想要的是绑定命令:
<MenuItem Command="{Binding DataContext.RunCommand}" />
there are many wonderfull sources of info out there... here is one .那里有许多很棒的信息来源…… 这是一个。
Thanks.., Well.谢谢..,嗯。 below piece of code worked for me.
下面的一段代码对我有用。
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem menuItem = (MenuItem)e.Source;
ContextMenu contextMenu = menuItem.CommandParameter as ContextMenu;
ListViewItem item = (ListViewItem)contextMenu.PlacementTarget;
var x = ((myViewModel)(item.Content)).myModel;
//'x' gives all required data of list view item
}
This is my XAML这是我的 XAML
<ListView.Resources>
<ContextMenu x:Key="GvRowMenu" ItemsSource="{Binding ContextMenuItems}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImagePath}"/>
<TextBlock Text="{Binding Name}"/>
<MenuItem Click="MenuItem_Click"
CommandParameter="{Binding
RelativeSource={RelativeSource
AncestorType={x:Type ContextMenu}}}"/>
</StackPanel>
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</ListView.Resources>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.