Passing a parameter using RelayCommand defined in the ViewModel (from Josh Smith example)

后端 未结 6 498
既然无缘
既然无缘 2020-12-09 02:57

I would like to pass a parameter defined in the XAML (View) of my application to the ViewModel class by using the RelayCommand. I followed Josh Smith\'s excellent article o

相关标签:
6条回答
  • 2020-12-09 03:20

    Nothing posted here before worked for me.

    Turns out, all answers are missing the <object> after RelayCommand!

    This works for me:

    public RelayCommand<object> OKCommand
    {
        get
        {
            if (_okCommand == null)
                _okCommand = new RelayCommand<object>(OkCommand_Execute);
            return _okCommand;
        }
    }
    private RelayCommand<object> _okCommand = null;
    
    private void OkCommand_Execute(object obj)
    {
        Result = true;
    }
    

    If you want to use aCanExecute method, use the following code:

    _okCommand = new RelayCommand<object>(OkCommand_Execute, OkCommand_CanExecute);
    
    private bool OkCommand_CanExecute(object obj) { }
    
    0 讨论(0)
  • 2020-12-09 03:24

    You'll pass the param in the lambda to the command like so:

    if (_aCommandWithAParameter == null)
    {           
        _aCommandWithAParameter = new RelayCommand(               
            param => this.CommandWithAParameter(param)
            );        
    }
    
    0 讨论(0)
  • 2020-12-09 03:25

    I don't understand why you have the extra complexity of specifying the lambda in the first place. Why not just do this:

    if (_aCommandWithAParameter == null)
    {           
        _aCommandWithAParameter = new RelayCommand<object>(CommandWithAParameter);
    }
    
    private void CommandWithAParameter(object state)
    {
        var str = state as string;
    }
    
    0 讨论(0)
  • 2020-12-09 03:27

    I am just trying to sell my point, check this out whether this works...

    http://mywpf-visu.blogspot.com/2009/12/relay-command-how-to-pass-parameter.html

    0 讨论(0)
  • 2020-12-09 03:36

    Here is a simple solution for the commandparameter as I was looking for help on the subject. I could not find anything online that was simple enough. The following solution works well when you are using a relaycommand. I had a few hyperlinks for which I needed to get the url value that was clicked using the command parameter.

    Step 1: In your relay command, create a simple property that will hold the parameter object value. You could call it parametervalue or any name that you prefer.

    public object ParameterValue
    {
      get;
      set;
    }
    

    Step 2: In the Execute Method of the RelayCommand class, set the value or the property created above to the parameter from the Execute method.

    readonly Action<object> m_execute;       // Action to execute
    
    public void Execute(object parameter)
     {
       this.ParameterValue = parameter;
       m_execute(parameter);
     }
    

    Step 3: Now when you can bind the CommandParameter in xaml to any value you want to retrieve when the command is executed. example:

    <TextBlock>
      <Hyperlink Command="{Binding Path=NavigateUrlCmd}"
                 CommandParameter="{Binding ElementName=tbwebsite, Path=Text}">
        <TextBlock Name="tbwebsite" Text="{Binding Path=website}"/>
      </Hyperlink>
    </TextBlock> 
    

    If you have a command called chickenCommand, when executed you could access the parameter: chickenCommand.ParameterValue

    I hope this helps somebody. Thank you for all your previous help.

    0 讨论(0)
  • 2020-12-09 03:39

    I cannot substitute a reference to the method name for the lamda expression withing a compile error. Apparently, and by no means surprisingly, a non-static method name reference cannot be used in place of a lambda. I hardly see it as "added complexity". Consistently passing lamdas makes sense to me.

    0 讨论(0)
提交回复
热议问题