C# 将参数传递给 ICommand

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13112557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:38:24  来源:igfitidea点击:

Passing a parameter to ICommand

c#wpf

提问by Michael Harper

I have a simple button that uses a command when executed, this is all working fine but I would like to pass a text parameter when the button is clicked.

我有一个简单的按钮,在执行时使用命令,这一切正常,但我想在单击按钮时传递一个文本参数。

I think my XAML is ok, but I'm unsure how to edit my RelayCommandclass to receive a parameter:

我认为我的 XAML 没问题,但我不确定如何编辑我的RelayCommand类来接收参数:

<Button x:Name="AddCommand" Content="Add" 
    Command="{Binding AddPhoneCommand}"
    CommandParameter="{Binding Text, ElementName=txtAddPhone}" />
public class RelayCommand : ICommand
{
    private readonly Action _handler;
    private bool _isEnabled;

    public RelayCommand(Action handler)
    {
        _handler = handler;
    }

    public bool IsEnabled
    {
        get { return _isEnabled; }
        set
        {
            if (value != _isEnabled)
            {
                _isEnabled = value;
                if (CanExecuteChanged != null)
                {
                    CanExecuteChanged(this, EventArgs.Empty);
                }
            }
        }
    }

    public bool CanExecute(object parameter)
    {
        return IsEnabled;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        _handler();
    }
}

采纳答案by McGarnagle

Change Actionto Action<T>so that it takes a parameter (probably just Action<object>is easiest).

更改ActionAction<T>使其接受一个参数(可能只是Action<object>最简单的)。

private readonly Action<object> _handler;

And then simply pass it the parameter:

然后简单地将参数传递给它:

public void Execute(object parameter)
{
    _handler(parameter);
}

回答by philu

You can simply do this (no change to RelayCommand or ICommand required):

您可以简单地执行此操作(无需更改 RelayCommand 或 ICommand):

private RelayCommand _addPhoneCommand;
public RelayCommand AddPhoneCommand
{
    get
    {
        if (_addPhoneCommand == null)
        {
            _addPhoneCommand = new RelayCommand(
                (parameter) => AddPhone(parameter),
                (parameter) => IsValidPhone(parameter)
            );
        }
        return _addPhoneCommand;
    }
}

public void AddPhone(object parameter)
{
    var text = (string)parameter;
    ...
}

public void IsValidPhone(object parameter)
    var text = (string)parameter;
    ...
}

回答by Jaken Herman

You could just do

你可以这样做

public ICommand AddPhoneCommand
{
    get
    {
        return new Command<string>((x) =>
        {
            if(x != null) { AddPhone(x); }
        };
    }
}

Then, of course have your AddPhone:

然后,当然有你的AddPhone

public void AddPhone(string x)
{
    //handle x
}