-1

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?

farzadwpf
  • 11
  • 3
  • `Action` encapsulates method that takes a parameter whereas your searchMethod does not take a paramater. They don't match. You need a constructor which takes `Action`. – emoacht Aug 01 '22 at 22:38

1 Answers1

0

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();
} 
BionicCode
  • 1
  • 4
  • 28
  • 44
  • actually none of them would return the one i need. ! so sorry. what esle should do? – farzadwpf Aug 02 '22 at 17:05
  • Why can't you give proper feedback? It would be really helpful if you could explain what is not working for you. It's frustrating that you kind of expect me to read your mind. My example was meat to show you how to correctly initialize a delegate, because you got the syntax wrong. I was hoping you can transfer the exampled to your actual problem. I have updated my answer to show how to modify the RelayCommand to accept a parameterless method, – BionicCode Aug 02 '22 at 18:04
  • actullay i didnt want to annoy @BionicCode but the truth is the answer is not helpful for me. again so sorry may be one could it find helpul. any way i follow this [link](https://www.youtube.com/watch?v=wpBRHNDFXYI) and ofcourse is not answerd with ralayCommand but it helps me to the one I need. filter datagrid – farzadwpf Aug 14 '22 at 17:46