1

Other examples are not working or are completly different from what I do.

So I have a button on my XAML page like this:

 <Button Width="100"
         Height="50"
         Margin="0 0 10 0"
         Command="{Binding MenuButtonViewModel.MenuButtonCommand, Source={StaticResource Locator}}"
         CommandParameter="{Binding Parameter}"
         IsEnabled="{Binding IsEnabled}">

And on my ViewModel this:

    public RelayCommand MenuButtonCommand
    {
        get
        {
            return new RelayCommand(() =>
            {
            });
        }
    }

The question is how do I get the value of the commandparameter on my ViewModel?

    public RelayCommand<String> MenuButtonCommand
    {
        get
        {
            return new RelayCommand((parameter) =>
            {
                 Text = parameter;
            });
        }
    }

This is not working, have no idea how to do this without having to use codebehind to pass the commandparameter value to the ViewModel.

Arie
  • 5,251
  • 2
  • 33
  • 54
user7998549
  • 131
  • 3
  • 15
  • This should work. Show what you have inside that `Parameter` property. – mrogal.ski Jun 21 '17 at 10:04
  • Your RelayCommand with the string parameter looks okay. That you do not get the parameter value could be caused by either the `{Binding Parameter}` binding failing, or the binding source providing a value/object that is not a string... –  Jun 21 '17 at 10:07
  • I get the error "Delegate 'Action' does not take 1 arguments". – user7998549 Jun 21 '17 at 10:08
  • 1
    Ah, yes... Try to initialize your RelayCommand like `new RelayCommand( ... )` –  Jun 21 '17 at 10:09
  • What is your implementation of RelayCommand? Have you tried this: https://stackoverflow.com/a/22286816/891715 – Arie Jun 21 '17 at 10:31
  • @Arie I'm using the MVVM Light libraries. – user7998549 Jun 21 '17 at 11:11

1 Answers1

0

Ok found the solution:

    public RelayCommand<string> Command
    {
        get
        {
            return new RelayCommand<string>(parameter =>
            {
                var str = parameter;
            });
        }
    }

Thanks guys.

user7998549
  • 131
  • 3
  • 15