MVVM Light RelayCommand Parameters

后端 未结 3 1470
[愿得一人]
[愿得一人] 2020-11-29 06:11

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 u

相关标签:
3条回答
  • 2020-11-29 06:45

    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)

    0 讨论(0)
  • 2020-11-29 06:47

    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();
    }
    
    0 讨论(0)
  • 2020-11-29 06:48

    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)
    {
    
    }
    
    0 讨论(0)
提交回复
热议问题