vendredi 18 décembre 2015

Filtering an ICollectionView on Multiple CheckBoxes

I currently have a list of 27,000 Companies that all fit into at least 1 of 3 categories, Subcontractors, Suppliers and Planthire. All of these companies are loaded into an ICollectionView, which is the ItemsSource for a DataGrid.

what I've previously been able to do in the past is filter the ICollectionView based on one property of a Model. For example if I had a list of jobs, and all jobs had an ITName property attached, I could do something like this;

    private void OnCheckBoxCheck(object sender, RoutedEventArgs e)
    {
        var checkedEmployees = new HashSet<string>();
        foreach (CheckBox checkBox in _employees.Children)
        {
            if (checkBox.IsChecked == true)
            {
                checkedEmployees.Add((string)checkBox.Content);
                checkedEmployees.Add((string)checkBox.Tag);
            }
        }

        JobsCollectionView.Filter =
           job => checkedEmployees.Contains((job as JobModel).ITName);        
    }

The Problem Now

The problem now is that I am trying to filter on multiple properties of a CompanyModel. With irrelevant parts taken out of it it looks like this;

public class CompanyModel
    {
        public int Subcontractor { get; set; }
        public int Supplier { get; set; }
        public int Planthire { get; set; }
    }

What I would like to do is if the Subcontractor CheckBox is checked but the others aren't, only show Subcontractors. If the Subcontractor and Supplier CheckBoxes are both checked, show Companies that fit into the Subcontractor AND Supplier categories. This is what I have attempted so far, however it does not achieve this;

    private void FilterCompanyType(object sender, RoutedEventArgs e)
    {
        var checkedCompanyFilters = new HashSet<string>();
        foreach (CheckBox checkBox in companyTypes.Children)
        {
            if (checkBox.IsChecked == true)
            {
                checkedCompanyFilters.Add((string)checkBox.Tag);
            }
        }

        if (subbieCheckBox.IsChecked == true)
        {
            CompanyICollectionView.Filter =
                company => checkedCompanyFilters.Contains((company as CompanyModel).Subcontractor.ToString());
        }

        if (supplierCheckBox.IsChecked == true)
        {
            CompanyICollectionView.Filter =
                company => checkedCompanyFilters.Contains((company as CompanyModel).Supplier.ToString());
        }

        if (planthireCheckBox.IsChecked == true)
        {
            CompanyICollectionView.Filter =
                company => checkedCompanyFilters.Contains((company as CompanyModel).Planthire.ToString());
        }          
    }

How can I modify this method so that it achieves what I would like it to?




Aucun commentaire:

Enregistrer un commentaire