0

I need help on 2 things.

  1. sending parameters (which is a static Enum) from a view to the viewmodel (I know I must use the CommandParameter on my button).

  2. How to delcare my DelegateCommand class so that it can accept the parameter.

Enumeration :

public static class Helpers
{
    public enum Operations
    {
        ONE,
        TWO
    }
}

ViewModel

public class SettingViewModel : ViewModelBase
{
    private DelegateCommand _UpdateCommand;

    public ICommand UpdateOperationValue
    {
        get
        {
            if (_UpdateCommand== null)
                _UpdateCommand= new DelegateCommand(Of Helpers.Operations)(param => UpdatePaint()); // Gives me erreur

            return _UpdateCommand;
        }
    }
}

View:

<Button Height="100" Width="100" Content="PEINTURE" 
            Background="{Binding PaintColorBrush}"
            Command="{Binding UpdateOperationValue}"
            CommandParameter="[... What do I do here ? ...]"/>

I've been searching on the web for solutions and came accross this :

<Button CommandParameter="{x:Static local:SearchPageType.First}" .../>

sadly in Universal Windows app I don't have the x:Static

My DelegateCommand class :

using System;
using System.Windows.Input;

public class DelegateCommand : ICommand
{
    /// <summary>
    /// Action to be performed when this command is executed
    /// </summary>
    private Action<object> _executionAction;

    /// <summary>
    /// Predicate to determine if the command is valid for execution
    /// </summary>
    private Predicate<object> _canExecutePredicate;

    /// <summary>
    /// CanExecuteChanged event handler
    /// </summary>
    public event EventHandler CanExecuteChanged;

    /// <summary>
    /// Initialize a new instance of the clDelegateCommand class
    /// The command will always be valid for execution
    /// </summary>
    /// <param name="execute">The delegate to call on execution</param>
    public DelegateCommand(Action<object> execute)
        : this(execute, null)
    { }

    /// <summary>
    /// Initializes a new instance of the clDelegateCommand class
    /// </summary>
    /// <param name="execute">The delegate to call on execution</param>
    /// <param name="canExecute">The predicate to determine if the command is valid for execution</param>
    public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
        {
            throw new ArgumentNullException("execute");
        }

        this._executionAction = execute;
        this._canExecutePredicate = canExecute;
    }

    /// <summary>
    /// Raised when CanExecute is changed
    /// </summary>
    public void RaiseCanExecuteChanged()
    {
        var handler = CanExecuteChanged;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }

    /// <summary>
    /// Execute the delegate backing this DelegateCommand
    /// </summary>
    /// <param name="parameter"></param>
    /// <returns>True if command is valid for execution</returns>
    public bool CanExecute(object parameter)
    {
        return this._canExecutePredicate == null ? true : this._canExecutePredicate(parameter);
    }

    /// <summary>
    /// Execute the delegate backing this DelegateCommand
    /// </summary>
    /// <param name="parameter">parameter to pass to delegate</param>
    /// <exception cref="InvalidOperationException">Thrown if CanExecute returns false</exception>
    public void Execute(object parameter)
    {
        if (!CanExecute(parameter))
        {
            throw new InvalidOperationException("The command is not valid for execution, check the CanExecute method before attempting to execute.");
        }

        this._executionAction(parameter);
    }
}
  1. How can I send a enum parameter from my view to my view model
  2. How can I instanciate a new DelegateCommand and receiving a enum as parameters.
Sam
  • 85
  • 3
  • 16

1 Answers1

1

How to delcare my DelegateCommand class so that it can accept the parameter.

You can use below class to accept parameters,

 public class DelegateCommand: ICommand
{
    #region Constructors       

    public DelegateCommand(Action<object> execute)
    : this(execute, null) { }

    public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    #endregion

    #region ICommand Members

    public event EventHandler CanExecuteChanged;

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

    public void Execute(object parameter)
    {
        if (_execute != null)
            _execute(parameter);
    }

    public void OnCanExecuteChanged()
    {
        CanExecuteChanged(this, EventArgs.Empty);
    }

    #endregion

    private readonly Action<object> _execute = null;
    private readonly Predicate<object> _canExecute = null;
}

In ViewModel ICommand Property example below,

public ICommand ExitCommand
    {
        get
        {
            if (exitCommand == null)
            {
                exitCommand = new DelegateCommand((obj)=>CloseApplication());
            }
            return exitCommand;
        }
    }

How can I send a enum parameter from my view to my view model

Commandparameter accept object. Bind your enum to CommandParameter and cast it in your ViewModel

Abin
  • 2,868
  • 1
  • 23
  • 59