45

I'm having an issue with passing a parameter to a relaycommand using the GalaSoft MVVM Light framework. I know that mvvm light's implementation of relaycommand doesn't use lambda parameters, so I did some research and found a way that people worked around it by doing something like this:

public RelayCommand ProjMenuItem_Edit
{
    get
    {
        if (_projmenuItem_Edit == null)
        {
            //This should work....
            _projmenuItem_Edit = new RelayCommand(ProjEditNode);
        }
        return _projmenuItem_Edit;
    }
}

private void ProjEditNode(object newText)
{
    var str = newText as string;
    OrganLocationViewModel sel = 
        ProjectOrganLocationView.GetExtendedTreeView().GetTopNode();

    //Console.WriteLine(sel.OrganDisplayName);
    sel.OrganDisplayName = str;
}

However, I keep getting an error on the line _projmenuItem_Edit = new RelayCommand(ProjEditNode); that says Argument 1: cannot convert from 'method group' to 'System.Action'

What am I missing?

Noctis
  • 11,507
  • 3
  • 43
  • 82
Saggio
  • 2,212
  • 6
  • 33
  • 50

3 Answers3

90

I believe this will work:

_projmenuItem_Edit = new RelayCommand<object>((txt)=>ProjEditNode(txt));

-- EDIT --

You'll need to define your RelayCommand with the type as well:

e.g.

public RelayCommand<string> myCommand { get; private set; }
myCommand = new RelayCommand<string>((s) => Test(s));

private void Test(string s)
{
    throw new NotImplementedException();
}
Noctis
  • 11,507
  • 3
  • 43
  • 82
Robaticus
  • 22,857
  • 5
  • 54
  • 63
  • 1
    I've seen this before and have tried it, but it looks like GalaSoft's RelayCommand doesn't include a RelayCommand implementation? I get this error when I try RelayCommand: Cannot implicitly convert type 'GalaSoft.MvvmLight.Command.RelayCommand' to 'GalaSoft.MvvmLight.Command.RelayCommand' – Saggio Mar 14 '11 at 13:30
  • Gah, I tried to edit but was too late... Disregard the previous comment, I forgot to change _projmenuItem_Edit to type RelayCommand too :p – Saggio Mar 14 '11 at 13:36
  • 3
    I updated the answer as well, for anyone else that's running into this. :) – Robaticus Mar 14 '11 at 13:38
  • 5
    one (unimportant) note - new RelayCommand((txt)=>ProjEditNode(txt)) can just be new RelayCommand(ProjEditNode) - .NET knows how to resolve that method and the parameter being passed to it. – Dave Rael Oct 31 '13 at 16:51
  • Damn ! This library needs a decent documentation... awesome stuff wihout knowing it exists is kind of... blazing ! – eka808 Sep 11 '14 at 15:03
  • 2
    A method group should work as well: myCommand = new RelayCommand(Test); – usefulBee Jun 12 '17 at 21:20
4

I don't think that RelayCommand() has a constructor that is not empty. you're trying to pass the wrong kind of method to it.

If you want the RelayCommand to support Command Parameters, you should use RelayCommand<T> where T can be any type of parameter. In your situation it would be RelayCommand<String> which would accept a method with void(string) signature. (and therefor would also be strongly typed and won't use the ugly object)

Elad Katz
  • 7,483
  • 5
  • 35
  • 66
2

Another way to declare relay commands, will help to reduce your code

public RelayCommand ChartCommand
{
    set
    {
        RelayCommand<string> chartCommand = 
            new RelayCommand<string>(e => ExecuteChartCommand(e));               
    }
}

public void ExecuteChartCommand(string vendor)
{

}
Noctis
  • 11,507
  • 3
  • 43
  • 82