【问题标题】:Universal Windows WPF CommandParameter bind to static Enumeration通用 Windows WPF CommandParameter 绑定到静态枚举
【发布时间】:2015-10-27 18:09:04
【问题描述】:

我在两件事上需要帮助。

  1. 将参数(这是一个静态枚举)从视图发送到视图模型(我知道我必须在按钮上使用 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

我的 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 并接收枚举作为参数。

【问题讨论】:

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


【解决方案1】:

如何删除我的 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 接受对象。将您的枚举绑定到您的CommandParametercast ViewModel

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 2011-08-17
    • 2016-03-26
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多