3

I have below xaml to to tree view control wiht contextmenu "Edit". When I select Edit context menu, my EditCommand method is executed in MVVM.

Problem:

I am getting parameter as "TreeView". I would like to get parameter as selected Item(where used did RMB)

 <TreeView x:Name="treeView" ItemsSource="{Binding TreeViewItems}">
   <TreeView.ContextMenu>
     <ContextMenu>
         <MenuItem Header="Edit" Command="{Binding EditCommand}" 
              CommandParameter="{Binding PlacementTarget, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ContextMenu}}}"/>
     </ContextMenu>
  </TreeView.ContextMenu>
</TreeView>

Can anybody guide me to what to change in CommandParameter to get Selected Item.

I have already tried below linked, but solution provided didn't work for me. [WPF treeview contextmenu command parameter [CommandParameters in ContextMenu in WPF

Community
  • 1
  • 1

1 Answers1

2

Just add SelectedItem to PlacementTarget like so:

<TreeView x:Name="treeView" ItemsSource="{Binding TreeViewItems}">
   <TreeView.ContextMenu>
      <ContextMenu>
          <MenuItem Header="Edit" Command="{Binding EditCommand}" 
              CommandParameter="{Binding PlacementTarget.SelectedItem, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ContextMenu}}}"/>
      </ContextMenu>
   </TreeView.ContextMenu>
</TreeView>

Update 1

ICommand implementation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfApplication1
{
  public class Command<T> : ICommand
  {
    private Action<T> _execute;
    private Predicate<T> _canExecute;

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public Command(Action<T> execute, Predicate<T> canExecute = null)
    {
        _execute = execute;
        _canExecute = canExecute;
    }
    public bool CanExecute(object parameter)
    {
        if (_canExecute == null) return true;
        return _canExecute((T)parameter);
    }

    public void Execute(object parameter)
    {
        _execute((T)parameter);
    }
  }
}

Code behind:

using System.Collections.Generic;
using System.Windows;
using System.Linq;
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using WpfApplication1;

namespace WPF_Sandbox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<string> Data { get; set; }  = new ObservableCollection<string>();
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            Data.Add("A");
            Data.Add("B");
            Data.Add("C");
        }

        public ICommand EditCommand
        {
            get
            {
                return new Command<object>(Edit);
            }
        }

        private void Edit(object param)
        {
            //Your code here
        }
    }    
}

This works for me.

user1286901
  • 219
  • 3
  • 10
  • No luck. Still I am getting Null value in EditCommand. Could you please verify , am I missing something. – dhiraj sakariya Feb 23 '17 at 16:15
  • @dhiraj sakariya - Silly question, are you making sure you left click an item first to select it, then right clicking? If you aren't, then that would explain why you are getting null parameter. If you are, then I would need to see your `ICommand` implementation. I have updated my answer to show my command implementation along with my code behind. Note: I normally do not place any code in code behind. Instead I place the code to bind to in a ViewModel. – user1286901 Feb 24 '17 at 03:10
  • Excellent diagnosis! I have just right clicked on item So it was not selected. 1. How I can get right clicked item in the commandParameter? 2. I have create usercontrol for each node in treeview and usercontrol is depend on Template. Code snippet: – dhiraj sakariya Feb 24 '17 at 06:48
  • I have created contextmenu at usercontrol, How can I bind EditCommand to TreeView's DataContext's viewmodel? How clicked item data be passed as an argument? – dhiraj sakariya Feb 24 '17 at 06:56