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?
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:
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: