0

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

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

chriga
  • 798
  • 2
  • 12
  • 29
rajcool111
  • 657
  • 4
  • 14
  • 36

2 Answers2

1

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.

Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
0

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

<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>
Indy9000
  • 8,651
  • 2
  • 32
  • 37
rajcool111
  • 657
  • 4
  • 14
  • 36
  • Just because you can paint a wall by throwing an open paint can at it doesn't mean that's the best way to do it. If I were you I would look at the other answer and research MVVM along with Command binding – DotNetRussell Jun 02 '15 at 17:09