通用Windows WPF CommandParameter绑定到静态枚举

时间:2015-10-27 18:09:05

标签: c# wpf mvvm visual-studio-2015 win-universal-app

我需要2件事的帮助。

  1. 从视图向viewmodel发送参数(这是一个静态枚举)(我知道我必须在我的按钮上使用CommandParameter)。

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

  3. 枚举:

    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

    我的DelegateCommand类:

    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. 如何从我的视图向我的视图模型发送枚举参数
    2. 如何实现新的DelegateCommand并接收枚举作为参数。

1 个答案:

答案 0 :(得分:1)

  

如何delcare我的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;
        }
    }
  

如何从我的视图向我的视图模型发送枚举参数

Commandparameter接受对象。将您的枚举绑定到CommandParameter

中的castViewModel