KeyInput绑定中的CommandParameter用法。按钮绑定

时间:2014-03-07 23:56:05

标签: c# wpf commandparameter

以下两个元素在我的ICommand实现中以不同方式触发并导致问题。当实现为TextBox输入CanExecuteChanged(object parameter)时,parameter的值为null。当它为Button输入相同的方法时,参数的值等于CommandParameter。

理想情况下,在两种情况下,我都希望CommandParameter值不会发送到CanExecuteChanged,而只发送到Execute。

ICommand的实施

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


        public bool CanExecute(object parameter)
        {
            if (parameter is bool)
            {
                this.canExecute = (bool)parameter;
            }

            return this.canExecute;
        }

        public void Execute(object parameter)
        {
            this.executeAction((T)parameter);
        }              


        internal void RaiseCanExecuteChanged()
        {           
            this.OnCanExecuteChanged();
        }


        private void OnCanExecuteChanged()
        {
            if (this.canExecuteChanged != null)
            {
                this.canExecuteChanged(this, EventArgs.Empty);
            }
        }

文本框

<TextBox Width="80" Margin="2,2,2,2" Text="{Binding LastName, UpdateSourceTrigger=PropertyChanged}" MaxLength="25">
                <TextBox.InputBindings>
                    <KeyBinding Key="Enter" Command="{Binding SearchCommand}">
                        <KeyBinding.CommandParameter>
                            <s:Boolean>True</s:Boolean>
                        </KeyBinding.CommandParameter>
                    </KeyBinding>
                </TextBox.InputBindings>
            </TextBox>

按钮

<Button Margin="2,2,2,2" Padding="10,0,10,0" Content="Search">
                <Button.InputBindings>
                    <MouseBinding Command="{Binding SearchCommand }" MouseAction="LeftClick">
                        <MouseBinding.CommandParameter>
                            <s:Boolean>True</s:Boolean>
                        </MouseBinding.CommandParameter>
                    </MouseBinding>
                </Button.InputBindings>
            </Button>

1 个答案:

答案 0 :(得分:0)

在这种情况下,请尝试ICommand的实施@JoshSmith,对我而言,两种选择都运作良好:

<强> RelayCommand

public class RelayCommand : ICommand
{
    private readonly Action<object> _execute;
    private readonly Predicate<object> _canExecute;

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

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
        {
            throw new ArgumentNullException("execute");
        }

        _execute = execute;
        _canExecute = canExecute;
    }

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

<强> SearchCommand

private RelayCommand _searchCommand = null;

public ICommand SearchCommand
{
    get
    {
        if (_searchCommand == null)
        {
            _searchCommand = new RelayCommand(param => this.Search(param), param => true);
        }

        return _searchCommand;
    }
}

private void Search(object param)
{
    bool parameter = (bool)param;

    if (parameter) 
    {
        MessageBox.Show("Pressed the Enter Key");
    }
}