Collectionview过滤复选框和文本框文本 [英] Collectionview filtering on checkboxes and textbox text

查看:62
本文介绍了Collectionview过滤复选框和文本框文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑过滤一个CollectionView,首先是基于TextBox内部的一些Text,其次是是否检查了某些CheckBox。我真正想要避免的是使用超长过滤方法,例如,

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
}





等等,但是我可能最终会使用多少个CheckBox。我甚至可能检查了多个CheckBox,因此最终可能会结束;





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 
}





真的可能会花很长时间。我可以就如何整齐地避免一种令人难以置信的长过滤方法提出一些建议吗?我希望我能正确解释自己。



我有什么试过:



这是我到目前为止使用的基于SearchBox文本过滤的方法;





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;
}

推荐答案

如果必须填写所有复选框条件(AND在一起),您至少可以减少问题每个复选框一个if语句,避免重复文本检查,方法如下:



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:

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.


这篇关于Collectionview过滤复选框和文本框文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆