Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM Light RelayCommand Parameters

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?

like image 444
Saggio Avatar asked Mar 14 '11 13:03

Saggio


People also ask

What is RelayCommand?

The RelayCommand and RelayCommand<T> are ICommand implementations that can expose a method or delegate to the view. These types act as a way to bind commands between the viewmodel and UI elements.

What is MVVM Light?

MVVM-light is a toolkit written in C# which helps to accelerate the creation and development of MVVM applications in WPF, Silverlight, Windows Store, Windows Phone and Xamarin.

What is MVVM command?

Commands are an implementation of the ICommand interface that is part of the . NET Framework. This interface is used a lot in MVVM applications, but it is useful not only in XAML-based apps.


1 Answers

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(); } 
like image 108
Robaticus Avatar answered Sep 22 '22 17:09

Robaticus