数据网格过滤,多输入,MVVM,C#

数据网格过滤,多输入,MVVM,C#,c#,wpf,mvvm,datagrid,filtering,C#,Wpf,Mvvm,Datagrid,Filtering,因此,我有一点困扰弄清楚这一点:我有一个数据网格,我正试图过滤。我正在使用ICollectionView作为ItemsSource。我有两个组合框,我想缩小数据范围,然后我还有一个文本框,我希望用户进一步缩小数据范围 我的问题是,我可以让文本框过滤数据,我可以让组合框过滤数据,但我不能让它们一起工作来进一步过滤数据。一次只有一个过滤器工作 public static string FilterText { get { return _filterText; } set {

因此,我有一点困扰弄清楚这一点:我有一个数据网格,我正试图过滤。我正在使用ICollectionView作为ItemsSource。我有两个组合框,我想缩小数据范围,然后我还有一个文本框,我希望用户进一步缩小数据范围

我的问题是,我可以让文本框过滤数据,我可以让组合框过滤数据,但我不能让它们一起工作来进一步过滤数据。一次只有一个过滤器工作

public static string FilterText
{
   get { return _filterText; }
   set
   {
      _filterText = value;
      ICollectionView.Filter = FilterTB;
   }
}

public static Model1 PublicModelProperty
{
   get { return _publicModelProperty; }
   set
   {
      _publicModelProperty = value;
      ICollectionView.Filter = FilterCB;
   }
}

public static bool FilterTB(object names)
{
   Model2 name = names as Model2;
   if(!string.IsNullOrEmpty(FilterText))
   {
      return name.Property1.Contains(FilterText) ||
             name.Property2.Contains(FilterText);
   }
   else
   {
      return true;
   }
}

public static bool FilterCB(object names)
{
   Model2 name = names as Model2;
   if(!string.IsNullOrEmpty(PublicModelProperty.Property))
   {
      return name.Property3.Contains(PublicModelProperty.Property);
   }
   else
   {
      return true;
   }
}

您应该同时使用FilterText和PublicModelProperty进行筛选

public static string FilterText
{
   get { return _filterText; }
   set
   {
      _filterText = value;
      ICollectionView.Filter = FilterBoth;
   }
}

public static Model1 PublicModelProperty
{
   get { return _publicModelProperty; }
   set
   {
      _publicModelProperty = value;
      ICollectionView.Filter = FilterBoth;
   }
}

public static bool FilterBoth(object names)
{
    Model2 name = names as Model2;
    if (!string.IsNullOrEmpty(FilterText))
    {
        if (!name.Property1.Contains(FilterText) &&
                !name.Property2.Contains(FilterText))
            return false;
    }
    if (!string.IsNullOrEmpty(PublicModelProperty.Property))
    {
        if (!name.Property3.Contains(PublicModelProperty.Property))
            return false;
    }
    return true;
}
谢谢你的回复:)我尝试了一下,但它并没有按照我的意愿运行。如果我使用组合框应用第一个过滤器,那么一切都会正常工作。但是,当我使用文本框进一步过滤我的结果时,它将添加到数据集中,而不是从组合框中过滤缩减的数据集。是否有一种方法可以显示所有过滤器输入(组合框和文本框)中仅满足条件的数据?另外,我应该注意,我在上面的代码中有一个类型o。FilterCB正在筛选a names.Property3。我编辑了上面的代码。我道歉。