5

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

View.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:

public MyView()
{
    InitializeComponent();
}

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

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 InputId when I click the button to my viewmodel?

0

2 Answers 2

11

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

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}" 
Sign up to request clarification or add additional context in comments.

2 Comments

This is perfect! Thank you.
After googling for 30 minutes, this was by far the best explanation. Thanks!
2

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

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.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.