1

I would like to ask, is it possible to pass string value from view (xaml) to property in ViewModel?

I have Two tabs. First is "Process" and the second is "Non-Process". Depends on that string value RelayCommand will execute and fire method with DispatcherTimer (if Process then Dispatcher1, if Non-Process then Dispatcher2).

xaml:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="PreviewMouseDown" >
       <cmd:EventToCommand Command="{Binding EmployeeViewM.MeasurementEndExecution}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

Can I use CommandParameter and CommandParameterValue to pass that string to the property?

Thank you for any suggestion

2
  • 1
    Before asking if you can, did you try it? Of course you can use CommandParameter, and it haven't to be a string, it can be anything you want and it can bind to any property you want. If you have 2 windows, in the first you can pass the string "process" and in the second "non-process". The method that the command raises has a parameter of type object, and it is exactly the parameter that the window passes to the VM. Commented May 31, 2016 at 19:43
  • What did you try? What happened? How was that different from what you wanted? Please provide a good minimal reproducible example that shows clearly what you're trying to do, along with a precise description of what specific problem you are having getting the code to work. Commented Jun 1, 2016 at 2:19

1 Answer 1

1

Yes you can, like below:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="PreviewMouseDown" >
        <cmd:EventToCommand Command="{Binding EmployeeViewM.MeasurementEndExecution}" CommandParameter="Process"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

And/OR

<i:Interaction.Triggers>
    <i:EventTrigger EventName="PreviewMouseDown" >
        <cmd:EventToCommand Command="{Binding EmployeeViewM.MeasurementEndExecution}" CommandParameter="Non-Process"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

Hopefully your RelayCommand (or ICommand implementation) is already accepting CommandParameter. Like below.

 public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    #endregion // Fields

    #region Constructors

    /// <summary>
    /// Creates a new command that can always execute.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    /// <summary>
    /// Creates a new command.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    /// <param name="canExecute">The execution status logic.</param>
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }

    #endregion // Constructors

    #region ICommand Members


    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

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

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

    #endregion // ICommand Members
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have 3 questions. 1. Why are we throwing ArgumentNullException if (if I understand correctly) we're calling this constructor from another that defaults execute to null? 2. What is CommandManager for? 3. If we were nitpicky, it would be better to create two separate classes for with and without Predicate, right?
@Whazz Your concerns are thoughful but I just picked the basic definition of the RelayCommand from MSDN as the emphasis was on the ability to pass the values to the command in ViewModel. Not on the usefulness of Command design.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.