-2

I'm trying to do a search filter on my program. I've already seen this post to understand how to start (Filtering ICollectionView binded to ItemsControl), but I have some errors that I can't fix.

public class SchedulingPageVm : PageViewModel
{
   private readonly NcManager _ncManager;
   private readonly Scheduler _scheduler;
   private String _ncResearch;
   public ICollection<Nc> Ncs => _ncManager.Ncs;

   public string _NcResearch
   {
       get { return _ncResearch; }
       set
       {
           _ncResearch = value;
           OnPropertyChanged("NcList");
       }
   }

   private RelayCommand _search;
   public RelayCommand Search => _search ??= new RelayCommand(_Search);

   public SchedulingPageVm(ICollection<JobDocument> ElencoNc)
   {
       InitializeComponent();
       ElencoNc = (ICollection<JobDocument>)_ncManager.Ncs;

       NcView.Filter = new Predicate<object>(object => _Search(object as _ncResearch));
       this.DataContext = _ncManager.Ncs;
   }

   private bool _Search(JobDocument elencoNc)
   {
       return _ncResearch == null
           || elencoNc.Code.IndexOf(_ncResearch, StringComparison.OrdinalIgnoreCase) != -1
           || elencoNc.FileName.IndexOf(_ncResearch, StringComparison.OrdinalIgnoreCase) != -1
           || elencoNc.FilePath.IndexOf(_ncResearch, StringComparison.OrdinalIgnoreCase) != -1;
   }

   public CollectionView NcView
   {
       get { return (CollectionView)CollectionViewSource.GetDefaultView(_ncManager.Ncs); }
   }

   public ObservableCollection<Nc> DataContext { get; }

Errors:

  1. public RelayCommand Search => _search ??= new RelayCommand(_Search);

    Argument 1: cannot convert from 'method group' to 'System.Action'

  2. NcView.Filter = new Predicate<object>(object,_Search(object as _ncResearch));

    The type or namespace name '_ncReasearch' could not be found (you are missing a using directive or an assembly reference)* *Error invalid expression term 'object'

thatguy
  • 21,059
  • 6
  • 30
  • 40
Matteo
  • 316
  • 1
  • 7
  • 21

1 Answers1

1

Argument 1: cannot convert from 'method group' to 'System.Action'

The RelayCommand expects a method signature like this.

void _Search(object obj)

Your _Search method does not match. You can either introduce a lambda to cast the object:

public RelayCommand Search => _search ??= new RelayCommand(obj => _Search((JobDocument)obj));

Or you can change the signature of the _Search method and cast the object in it, but I guess you need this method to return a bool for the filter, so you cannot do this.

private bool _Search(object obj)
{
   var elencoNc = (JobDocument) obj;
   // ...other code.
}

However, the main question here is why you create a command for this in the first place. The command does not return anything and your _Search method is a predicate, so it does not change any state. The invocation of the command does not do anything except waste CPU time.

The type or namespace name '_ncReasearch' could not be found (you are missing a using directive or an assembly reference) Error invalid expression term 'object'

You are creating the Predicate<object> wrong. A predicate of that type gets an object instance as parameter and returns a bool as result of its check. On the left side of the as operator needs to be an instance and on the right side the type to cast to, here JobParameter.

NcView.Filter = new Predicate<object>(obj => _Search(obj as JobDocument));

You could even cast the obj directly like (JobDocument) obj and assign the Filter directly.

NcView.Filter = obj => _Search((JobParameter) obj);

Read more on the as operator in the documentation: as operator

thatguy
  • 21,059
  • 6
  • 30
  • 40