c# - Key Input Bindings 与 CommandParameter 用法伯顿绑定(bind)

标签 c# wpf commandparameter

以下两个元素在我的 ICommand 实现中以不同方式触发并导致问题。当实现为TextBox输入CanExecuteChanged(object parameter)时,parameter的值为null。当它为Button 进入同样的方法时,parameter 的值等于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>

最佳答案

在这种情况下,尝试@JoshSmithICommand 实现,对我来说这两个选项都很好:

中继命令

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

搜索命令

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

关于c# - Key Input Bindings 与 CommandParameter 用法伯顿绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22262986/

相关文章:

c# - Azure 移动应用主 Controller

c# - ModelVisual3D 无法在 DockPanel 中正确呈现

android - MVV交叉: Pass an enum value as a CommandParameter for Android

c# - 如何通过 XAML 中的 CommandParameter 传递当前聚焦的元素名称?

c# - 如何让 sitecore 显示图像(或其他资源)

C# 将所需的 DLL 放在输出根目录以外的位置

c# - 由于抽象方法,NHibernate 创建代理实例失败

wpf - 如何使用 token 在 MVVM light 中发送简单的字符串消息

.net - WPF 仅在 Debug模式下显示控件