wpf 使用命令将参数传递到视图模型中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35625533/
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-09-13 13:41:31  来源:igfitidea点击:

Pass parameter with a command into the viewmodel

c#wpfxamlmvvmdata-binding

提问by Rasmus B?kgaard

I have trouble sending a parameter from my view to my viewmodel.

我无法将参数从我的视图发送到我的视图模型。

View.xaml:

查看.xaml

In my view, I have the following:

在我看来,我有以下几点:

<TextBox
    MinWidth="70"
    Name="InputId"/>

<Button 
    Command="{Binding ButtonCommand}"
    CommandParameter="{Binding ElementName=InputId}"
    Content="Add"/>

View.xaml.cs:

查看.xaml.cs

public MyView()
{
    InitializeComponent();
}

public MyView(MyViewModel viewModel) : this()
{
    DataContext = viewModel;
}

MyViewModel.cs:

MyViewModel.cs

public class MyViewModel : BindableBase
{
    public ICommand ButtonCommand { get; private set; }

    public MyViewModel()
    {
        ButtonCommand = new DelegateCommand(ButtonClick);
    }

    private void ButtonClick()
    {
        //Read 'InputId' somehow. 
        //But DelegateCommand does not allow the method to contain parameters.
    }
}

Any suggestions to, how I can pass the InputIdwhen I click the button to my viewmodel?

有什么建议,我如何InputId在单击按钮时将其传递给我的视图模型?

回答by Johannes

You need to add <object>to your delegate command like this :

您需要<object>像这样添加到您的委托命令中:

public ICommand ButtonCommand { get; private set; }

     public MyViewModel()
        {
            ButtonCommand = new DelegateCommand<object>(ButtonClick);
        }

        private void ButtonClick(object yourParameter)
        {
            //Read 'InputId' somehow. 
            //But DelegateCommand does not allow the method to contain parameters.
        }

Do you want to get the text of textbox change your xaml to :

您想将文本框的文本更改为:

CommandParameter="{Binding Text,ElementName=InputId}" 

回答by Nawed Nabi Zada

For proper implementation of ICommand/RelayCommand have a look at this MSDNpage.

要正确实现 ICommand/RelayCommand,请查看此MSDN页面。

Summary:

概括:

public class RelayCommand : ICommand
{
    readonly Action<object> _execute;
    readonly Func<bool> _canExecute;

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

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

        _execute = execute;
        _canExecute = canExecute;
    }


    public bool CanExecute(object parameter)
    {
        return _canExecute == null || _canExecute.Invoke();
    }

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

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

} 


public MyViewModel()
{
    ButtonCommand = new RelayCommand(ButtonClick);
}

private void ButtonClick(object obj)
{
    //obj is the object you send as parameter.
}