【问题标题】:Adding More than One Criteria to a Predicate List向谓词列表添加多个条件
【发布时间】:2015-12-24 09:07:38
【问题描述】:

我有Companies,我正在尝试使用criteria 过滤。每个Company 都有一个CurrentStatus,并且每次用户检查CheckBox 以定义过滤器时都会调用过滤方法。除了一件事之外,我目前几乎完全按照我想要的方式工作。这就是我所拥有的;

    private void FilterCompanyType(object sender, RoutedEventArgs e)
    {
        criteria.Clear();

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

        if (nonCurrentCheckBox.IsChecked == true)
        {
            criteria.Add(new Predicate<CompanyModel>(x => x.CurrentStatus == 0));
        }

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

正如我所说,这工作正常,它适用于当用户想要查看Companies 的列表CurrentStaus == 1Companies 的列表CurrentStatus == 0。然而,目前用户看不到Companies的列表,如果CheckBoxesCurrentStatus == 0CurrentStatus == 1的位置被选中。

我已经尝试添加这个,但它不适用于 CheckBoxes 选中;

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

这只是返回一个空的DataGrid。如何更改 Predicate 以允许两者?

【问题讨论】:

  • 你可以试试criteria.Add( new Predicate&lt;CompanyModel&gt;( x =&gt; ( x.CurrentStatus == 1 || x.CurrentStatus == 0 ) ) ) 两者都被检查了。
  • @bars222 不幸的是,使用它仍然导致Companies 没有显示在DataGrid
  • 您还应该将代码添加到之前的if 语句中。 if (currentCheckBox.IsChecked == true && nonCurrentCheckBox.IsChecked == false)

标签: c# wpf predicate


【解决方案1】:

如果我清楚地理解,if 语句应该是这样的。

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 == 0 || x.CurrentStatus == 1 ) ) );
}

【讨论】:

    猜你喜欢
    • 2018-07-28
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    相关资源
    最近更新 更多