wpf The relayCommand needs a constractor that takes parameter

181 Views Asked by At

my goal is filter a dataGrid with a textbox input, so i have textbox to filter a datagrid. for all the other commands i used one of these constractors in my **relayCommand ** as follows:

// Constructors

  public RelayCommand(Action<object> action) 
    {
        _execute = action;
    }

  public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

now to do filter datagrid based on a textbox input i make a searchMethod in my viewModel as follows:

private ObservableCollection<Coach> searchMethod()
    {
        
            return CoachManage.GetCoachBySearch(TextToFilter);
        
    }

and also a Command in my viewModel as follows:

public ICommand SearchCommand
    {
        get
        {
            if (_searchCommand == null)
            {
                _searchCommand = new RelayCommand(new Action<object>(searchMethod()));
            }
            return _searchCommand;
        }
        set
        {
            SetProperty(ref _searchCommand, value);
        }
    }

and finally my textbox binded to the property of the one in my viewModel in my View:

<TextBox  x:Name="txtSearch" 
                 Margin="7,3,3,3" Grid.Row="1" Grid.Column="1"
                  Text="{Binding TextToFilter,  UpdateSourceTrigger=PropertyChanged}"                   
              Style="{StaticResource TextBoxStyle}"
               />

but the error says in following code:

*_searchCommand = new RelayCommand(new Action<object>(SearchCoach()));*

Method name expected

it seems that I should make new constructor in my relayCommand class which takes parameter to pass it in my Method. is that right and how could i do that?

1

There are 1 best solutions below

3
BionicCode On

You must register the delegate properly (the issue is not related to the constructor).

Here are some variations that all yield the same result, but more or less elegant. Some use the shortest form, method groups. Some use lambda expressions:

// Very verbose using method groups
var searchCommand = new RelayCommand(new Action<object>(SearchCoach));
// Very compact using method groups
var searchCommand = new RelayCommand(SearchCoach);
// Using a lambda expression
var searchCommand = new RelayCommand(commandParameter => SearchCoach(commandParameter));
// Using method groups
Action<object> commandDelegate = SearchCoach;
var searchCommand = new RelayCommand(commandDelegate);
// Using a lambda expression
Action<object> commandDelegate = commandParameter => SearchCoach(commandParameter);
var searchCommand = new RelayCommand(commandDelegate);

And many more, for example using an anonymous method.

If you want to use a parameterless void delegate you would have to add a corresponding constructor overload to your RelayCommand:

private Action _executeParameterless;
public RelayCommand(Action parameterlessAction) 
{
  _executeParameterless = parameterlessAction;
}

public void Execute(object commandParameter)
{
  _execute?.Invoke(commandParameter);
  _executeParameterless?.Invoke();
}