Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 筛选和ICollectionView筛选WPF_C#_Wpf_Filter_Datagrid - Fatal编程技术网

C# 筛选和ICollectionView筛选WPF

C# 筛选和ICollectionView筛选WPF,c#,wpf,filter,datagrid,C#,Wpf,Filter,Datagrid,我目前有一个DataGrid,里面装满了我根据两件事筛选的公司——一个是搜索框,用户可以根据公司名称、城镇或邮政编码进行筛选。这看起来像这样, 按姓名/城镇/邮政编码过滤 private void FilterDataGrid() { try { var searchText = CharactersOnly(searchBox.Text); Compa

我目前有一个
DataGrid
,里面装满了我根据两件事筛选的公司——一个是搜索框,用户可以根据公司名称、城镇或邮政编码进行筛选。这看起来像这样,

按姓名/城镇/邮政编码过滤

        private void FilterDataGrid()
        {
            try
            {
                var searchText = CharactersOnly(searchBox.Text);
                CompanyICollectionView.Filter = (obj =>
                {
                    CompanyModel compDetails = obj as CompanyModel;
                    if (compDetails == null)
                    {
                        return true;
                    }

                    if (compNameRad.IsChecked == true)
                    {
                        return CompanyContains(compDetails.Name, searchText.ToLower());
                    }
                    if (compTownRad.IsChecked == true)
                    {
                        return CompanyContains(compDetails.Town, searchText.ToLower());
                    }
                    if (compPcodeRad.IsChecked == true)
                    {
                        return CompanyContains(compDetails.Postcode, searchText.ToLower());
                    }
                    return false;
                });

                if (dataGrid.Items.Count == 0) // There are no companies with this filter on, clear the label
                {
                    compDetailsLabel.Content = string.Empty;
                }
                else
                {
                    dataGrid.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                var hEs = new HandleExceptionService();
                hEs.HandleException(ex.ToString());
            }
        }
    private void FilterCompanyType(object sender, RoutedEventArgs e)
    {
        criteria.Clear();

        if (currentCheckBox.IsChecked == true && nonCurrentCheckBox.IsChecked == false)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.CurrentStatus == 1));
        }
        else if (nonCurrentCheckBox.IsChecked == true && currentCheckBox.IsChecked == false)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.CurrentStatus == 0));
        }
        else if (nonCurrentCheckBox.IsChecked == true && currentCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => (x.CurrentStatus == 1 || x.CurrentStatus == 0)));
        }

        if (subbieCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.Subcontractor == 1));
        }

        if (supplierCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.Supplier == 1));
        }

        if (planthireCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.Planthire == 1));
        }

        if (architectCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.Architect == 1));
        }

        if (qsCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.QS == 1));
        }

        if (projectManagerCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.ProjectManager == 1));
        }

        if (structEngCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.StructEng == 1));
        }

        if (servEngCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.ServiceEng == 1));
        }

        foreach (CheckBox checkBox in companyFilters.Children)
        {
            if (!CheckCheckBoxes())
            {
                dataGrid.ItemsSource = null;
                compDetailsLabel.Content = string.Empty;
            }
            else
            {
                dataGrid.ItemsSource = CompanyICollectionView;
                CompanyICollectionView.Filter = dynamic_Filter;
                SetSelectedCompany(selectedIndex);
                dataGrid.SelectedIndex = 0;
            }
        }

        var nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
        nfi.NumberGroupSeparator = ",";
        numberOfCompaniesLabel.Content = "Number of Companies: " + dataGrid.Items.Count.ToString("#,#", nfi);
    }
第二种过滤方法基于公司类型。这是通过选择许多
复选框来完成的。这个方法看起来像这样

按公司类型筛选

        private void FilterDataGrid()
        {
            try
            {
                var searchText = CharactersOnly(searchBox.Text);
                CompanyICollectionView.Filter = (obj =>
                {
                    CompanyModel compDetails = obj as CompanyModel;
                    if (compDetails == null)
                    {
                        return true;
                    }

                    if (compNameRad.IsChecked == true)
                    {
                        return CompanyContains(compDetails.Name, searchText.ToLower());
                    }
                    if (compTownRad.IsChecked == true)
                    {
                        return CompanyContains(compDetails.Town, searchText.ToLower());
                    }
                    if (compPcodeRad.IsChecked == true)
                    {
                        return CompanyContains(compDetails.Postcode, searchText.ToLower());
                    }
                    return false;
                });

                if (dataGrid.Items.Count == 0) // There are no companies with this filter on, clear the label
                {
                    compDetailsLabel.Content = string.Empty;
                }
                else
                {
                    dataGrid.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                var hEs = new HandleExceptionService();
                hEs.HandleException(ex.ToString());
            }
        }
    private void FilterCompanyType(object sender, RoutedEventArgs e)
    {
        criteria.Clear();

        if (currentCheckBox.IsChecked == true && nonCurrentCheckBox.IsChecked == false)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.CurrentStatus == 1));
        }
        else if (nonCurrentCheckBox.IsChecked == true && currentCheckBox.IsChecked == false)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.CurrentStatus == 0));
        }
        else if (nonCurrentCheckBox.IsChecked == true && currentCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => (x.CurrentStatus == 1 || x.CurrentStatus == 0)));
        }

        if (subbieCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.Subcontractor == 1));
        }

        if (supplierCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.Supplier == 1));
        }

        if (planthireCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.Planthire == 1));
        }

        if (architectCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.Architect == 1));
        }

        if (qsCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.QS == 1));
        }

        if (projectManagerCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.ProjectManager == 1));
        }

        if (structEngCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.StructEng == 1));
        }

        if (servEngCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.ServiceEng == 1));
        }

        foreach (CheckBox checkBox in companyFilters.Children)
        {
            if (!CheckCheckBoxes())
            {
                dataGrid.ItemsSource = null;
                compDetailsLabel.Content = string.Empty;
            }
            else
            {
                dataGrid.ItemsSource = CompanyICollectionView;
                CompanyICollectionView.Filter = dynamic_Filter;
                SetSelectedCompany(selectedIndex);
                dataGrid.SelectedIndex = 0;
            }
        }

        var nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
        nfi.NumberGroupSeparator = ",";
        numberOfCompaniesLabel.Content = "Number of Companies: " + dataGrid.Items.Count.ToString("#,#", nfi);
    }

你是对的,问题是你每次都在重置过滤器。因此,您需要进行一些修改以解决问题(我假设所有代码都在同一个类中):

私有布尔过滤器公司信息(对象o){
//如果选择的信息和过滤器(名称、城镇、代码等)对应于对象o(铸造为公司模型),则返回true的函数
//我让你写这部分,应该像filterCompanyType方法一样。
}
私有布尔筛选器公司类型(对象o){
标准。清除();
if(currentCheckBox.IsChecked==true&&nonCurrentCheckBox.IsChecked==false)
{
Add(新谓词(x=>x.CurrentStatus==1));
}
else if(nonCurrentCheckBox.IsChecked==true&¤tCheckBox.IsChecked==false)
{
Add(新谓词(x=>x.CurrentStatus==0));
}
else if(nonCurrentCheckBox.IsChecked==true&¤tCheckBox.IsChecked==true)
{
添加(新谓词(x=>(x.CurrentStatus==1 | | x.CurrentStatus==0));
}
//……这里的所有其他标准
CompanyModel company=o作为CompanyModel;
bool-isIn=true;
if(criteria.Count()==0)
返回isIn;
isIn=criteria.TrueForAll(x=>x(公司));
返回isIn;
}
私人布尔过滤器公司(对象o){
返回filterCompanyType(o)和filterCompanyInfos(o)
}
public void ApplyFilter(集合视图公司集合视图){
CompanyICollectionView.Filter=this.FilterCompany;
//做一些其他的事情,比如选择索引。。。
}

无需在每次属性更改时重新创建筛选器。 最好的方法是将两个过滤器组合在一个函数中,将其传递给
companyCollectionView.Filter
,并从属性更改事件调用
companyCollectionView.Refresh()。
您甚至可以将collectionview绑定到支持筛选的ObservableCollection,即FilteredObservableCollection。。。
看看:

两个筛选器是否使用相同的项目源?
DataGrid
具有相同的
ItemsSource
是,(
companyCollectionView
)您的
动态\u筛选器从何而来?根据
标准创建
?添加了一个编辑。感谢您的回答。我对过滤公司信息有点不确定。这是否与我的方法相同,用户可以搜索姓名、城镇、代码等?或者该部分是否也需要位于
FilterCompanyType
中?filterCompanyInfos方法是关于按名称/城镇/邮政编码过滤。您需要对其进行调整,以便在尊重过滤器时,此函数为对象返回true我如何比较对象以确保其与已选择的过滤器匹配?