通用Windows WPF命令参数绑定到静态枚举

本文关键字:静态 枚举 绑定 参数 Windows WPF 命令 通用 | 更新日期: 2023-09-27 18:19:25

我有两件事需要帮助。

  1. 从视图发送参数(这是一个静态Enum)到视图模型(我知道我必须在我的按钮上使用CommandParameter)。

  2. 如何声明我的DelegateCommand类,以便它可以接受参数

枚举:

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

视图模型

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;
        }
    }
}

视图:

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

我一直在网上寻找解决方案,偶然发现了这个:

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

遗憾的是,在通用Windows应用程序中,我没有x:Static

我的delegate命令类:

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. 如何从我的视图发送enum参数到我的视图模型
  2. 如何实例化一个新的DelegateCommand和接收一个enum作为参数

通用Windows WPF命令参数绑定到静态枚举

如何声明我的DelegateCommand类,以便它可以接受参数。

你可以使用下面的类来接受参数,

 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;
}

在下面的ViewModel ICommand属性示例中,

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

如何从我的视图发送一个enum参数到我的视图模型

Commandparameter接受对象。在ViewModel

中将枚举绑定到CommandParametercast