Search code examples
c#.netwpfmvvmcollectionviewsource

CollectionViewSource Filter a Collection view with a list of items


i will try to explain my problem best i can, i have my collection view:

 CollectionViewSource ArticoliProduzioneViewSource;

that need to be filtered by 3 list of items

 public ObservableCollection<BLL.Models.Serie> ListaSerie { get; set; } = new ObservableCollection<BLL.Models.Serie>();  

 public ObservableCollection<BLL.Models.Prodotto> ListaProdotti { get; set; } = new ObservableCollection<BLL.Models.Prodotto>();

 public ObservableCollection<BLL.Models.Colore> ListaColori { get; set; } = new ObservableCollection<BLL.Models.Colore>();

enter image description here

this is the screen of what i need to do, only to let you understand, i need to filter the items inside the first datagrid(the collectionviewsource) with the items in the 3 smaller datagrids (the 3 observablecollections that i have figured before)

    private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
    {
        e.Accepted = 
        (ListaSerie.Count != 0 ? ((BLL.Models.ArticoloProduzione)e.Item).ArticoloSerieProdotto.Serie.Id == Serie.Id : true)
        &
        (ListaProdotti.Count != 0 ? ((BLL.Models.ArticoloProduzione)e.Item).ArticoloSerieProdotto.Prodotto.Id == Prodotto.Id : true)
        &
        (ListaColori.Count != 0 ? ((BLL.Models.ArticoloProduzione)e.Item).Colore.Id == Colore.Id : true)
        ;

    }

this is what i tried, it works, but it filters the main collectionview only with the last item that i have inserted in one of the 3 filter list.

what i need to do here is just to filter the main CollectionView with all the items that are included in the 3 observable collections that i use as filters. hope you understand, thank you!


Solution

  • Just Found The Solution to my question, this is it! hope it helps someone!

     public bool ControlloSuSerie(int idarticolo)
            {
                bool risultato = false;
                   for (int i = 0; i <= ListaSerie.Count - 1; i++)
                  {
                    if (ListaSerie[i].Id == idarticolo)
                    {
                        return true;
                    }
                    else
                    {
                        risultato =  false;
                    }
                }
                return risultato;
            }
            public bool ControlloSuProdotto(int idarticolo)
            {
                bool risultato = false;
                for (int i = 0; i <= ListaProdotti.Count - 1; i++)
                {
                    if (ListaProdotti[i].Id == idarticolo)
                    {
                        return true;
                    }
                    else
                    {
                        risultato = false;
                    }
                }
                return risultato;
            }
    
            public bool ControlloSuColore(int idarticolo)
            {
                bool risultato = false;
                for (int i = 0; i <= ListaColori.Count - 1; i++)
                {
                    if (ListaColori[i].Id == idarticolo)
                    {
                        return true;
                    }
                    else
                    {
                        risultato = false;
                    }
                }
                return risultato;
            }
    
            private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
            {
                e.Accepted = (CodiceArticolo != null && CodiceArticolo != "" ? ((BLL.Models.ArticoloProduzione)e.Item).Codice == CodiceArticolo : true) &
                     (ListaSerie.Count != 0 ? (ControlloSuSerie(((BLL.Models.ArticoloProduzione)e.Item).ArticoloSerieProdotto.Serie.Id)) : true)&
                     (ListaProdotti.Count != 0 ? (ControlloSuProdotto(((BLL.Models.ArticoloProduzione)e.Item).ArticoloSerieProdotto.Prodotto.Id)) : true)&
                     (ListaColori.Count != 0 ? (ControlloSuColore(((BLL.Models.ArticoloProduzione)e.Item).Colore.Id )) : true);
            }