Click here to Skip to main content
15,888,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm looking at Filtering a CollectionView based on firstly, some Text inside a TextBox and secondly whether certain CheckBoxes are checked or not. What I really want to avoid is having a super long filter method that says something like,
If CheckBox1 is checked
{
    If CompanyContains SearchBox.Text && CompanyMeetsCheckBox1Spec
}
If CheckBox2 is checked
{
    If CompanyContains SearchBox.Text && CompanyMeetsCheckBox2Spec
}


and so on and so forth for however many number of CheckBoxes I may end up with. I may even have multiple CheckBoxes checked so could end up with;

If CheckBox1 is checked && CheckBox2 is checked
{
    If CompanyContains SearchBox.Text && CompanyMeetsCheckBox1Spec &&                 CompanyMeetsCheckBox2Spec 
}


It really could get quite a long. Could I ask for some suggestion on how to "neatly" avoid an incredibly long filtering method?.. I hope I explained myself correctly.

What I have tried:

This is the method I am using so far to filter based on SearchBox Text;

private void SearchBoxCallApplyFilters(object sender, TextChangedEventArgs e)
{
    CollectionViewSource.GetDefaultView(Companies).Refresh();
}

private bool FilterCompanies(object obj)
{
    Var company = obj as CompanyModel;
    if (searchBox.Text == string.Empty)
    {
        return true;
    }
    else if (company.Name.ToLower().Contains(CharactersOnly(searchBox.Text.ToLower())))
    {
        return true;
    }
    return false;
}
Posted
Updated 7-Sep-16 6:53am
Comments
Ralf Meier 7-Sep-16 14:07pm    
For me your requirement is complete unclear - sorry ...
Please explain with an example what you try to achieve.

Of course it is possible to get results with a loop. For this it is necessary that corresponding controls should have corresponding names. Do you have it like this ?

1 solution

Provided that all checkbox conditions must be fullfilled ("ANDed" together) you at least can reduce the problem to one if statement per checkbox and avoid repetition of the text check in the following way:

C#
private bool FilterCompanies(object obj)
{
    Var company = obj as CompanyModel;
    if (searchBox.Text == string.Empty)
    {
        return true;
    }
    else if (company.Name.ToLower().Contains(CharactersOnly(searchBox.Text.ToLower())))
    {
        if( chkBox1.IsChecked && MeetsCheckBox1Spec(Company) == false )
        {
           return false;
        }
        if( chkBox2.IsChecked && MeetsCheckBox2Spec(Company) == false )
        {
           return false;
        }
        ...

        return true;
    }
    return false;
}


If you have more checkboxes that can be handled in this way or have more complex logic between you may have to think about whether your solution is user-friendly at all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900