如何在一个集合视图源上一个接一个地应用多个过滤函数(AND关系) [英] How do you apply multiple filter functions on one collection view source, one after another (AND relation)

查看:193
本文介绍了如何在一个集合视图源上一个接一个地应用多个过滤函数(AND关系)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用一个绑定到listview的Collection View Source对象,而CVS是Observable Collection对象顶部的一个视图。



我知道如何使用以下技术来应用过滤器:

  cvs.Filter + = new FilterEventHandler(SomeFilterFunction); 

当您仅在一个函数中进行筛选时,此工作正常。问题是当我想过滤已经过滤的CVS的顶部。如果我有另外一个函数根据不同的标准过滤视图中的对象,那么这些对象只会在第二个过滤器中的条件中被过滤,而第一个过滤器的结果将消失。



下面是一些示例代码来解释我的问题:

$ p code $ cvs.Filter + = new FilterEventHandler(SomeFilterFunction1);
cvs.Filter + = new FilterEventHandler(SomeFilterFunction2);

public void SomeFilterFunction1(object sender,FilterEventArgs e)
{
SomeObject example = e.Item as SomeObject;
if(example.Name.Contains(A))
{
e.Accepted = true;
}
else
{
e.Accepted = false;



$ public void SomeFilterFunction2(object sender,FilterEventArgs e)
{
SomeObject example = e.Item as SomeObject;
if(example.Name.Contains(B))
{
e.Accepted = true;
}
else
{
e.Accepted = false;






$ b所以在这个例子中,我只想要SomeObjects字母A和B被过滤器接受。我的问题是,当使用filterfunction2调用cvs + = Filter时,只接受包含字母B的对象名称,不考虑字母A的对象。因此,包含字母B但不是A的对象名称被接受, 。

我目前解决这个问题的方法是创建一个主过滤器函数,其中包含了每个过滤器函数,并且每个过滤器运行每个对象,如果对象通过所有的过滤器,然后它被接受。这确实有用,但是现在我的代码变得疯狂了,逻辑失控了。有谁知道如何对CVS的最后一个过滤器的结果应用新的过滤器功能?为什么CVS不会自动执行此操作,而不是通过每个过滤器发送每个对象,或者我没有正确地考虑CVS?你可以这样做:

  cvs.Filter + = new FilterEventHandler(SomeFilterFunction1); 
cvs.Filter + = new FilterEventHandler(SomeFilterFunction2);

public void SomeFilterFunction1(object sender,FilterEventArgs e)
{
SomeObject example = e.Item as SomeObject;
e.Accepted& = example.Name.Contains(A);
//如果你喜欢OR逻辑使用这个:
//e.Accepted | = example.Name.Contains(A);


$ b public void SomeFilterFunction2(object sender,FilterEventArgs e)
{
SomeObject example = e.Item as SomeObject;
e.Accepted& = example.Name.Contains(B);
//如果你喜欢OR逻辑使用这个:
//e.Accepted | = example.Name.Contains(B);



$ b $ p
$ b

这样做的是do和AND(或者OR,如果你使用| =运算符)与Accepted属性中的当前值,使您的过滤器一起工作。



编辑:这是在.NET 4.5中创建的。 b

I have been working with a Collection View Source object that is bound to a listview and the CVS is a view on top of Observable Collection of objects.

I know how to apply a filter using the following technique:

cvs.Filter += new FilterEventHandler(SomeFilterFunction);

This works fine when you are only filtering in one function. The problem is when I want to filter on top of the already filtered CVS. If I have another function that filters the objects in the view based on different criteria, the objects are filtered ONLY on the criteria in the second filter and the results of the first filter go away.

Here is some example code to explain my problem:

cvs.Filter += new FilterEventHandler(SomeFilterFunction1);
cvs.Filter += new FilterEventHandler(SomeFilterFunction2);

public void SomeFilterFunction1(object sender, FilterEventArgs e)
{
      SomeObject example = e.Item as SomeObject;
      if(example.Name.Contains("A"))
      {
          e.Accepted = true;
      }
      else
      {
          e.Accepted = false;
      }

}

public void SomeFilterFunction2(object sender, FilterEventArgs e)
{
      SomeObject example = e.Item as SomeObject;
      if(example.Name.Contains("B"))
      {
          e.Accepted = true;
      }
      else
      {
          e.Accepted = false;
      }
}

So in this example, I want only "SomeObjects" with the letters A and B accepted by the filters. My problem is that When make the call cvs+=Filter with the filterfunction2, only Objects Name containing the letter B are accepted, disregarding the objects with letter A. So an Objects name containing the letter B but not A are accepted, when they shouldnt be.

My current solution to this problem has been creating a "Master" filter function that has every filter function inside of it and I run every Object through every filter and if the object makes it through ALL of the filters then it is accepted. This does work but my code is getting crazy now and logic is getting out of control. Does anyone know how to apply a new filter function on the result of the last filter for a CVS? Why doesn't the CVS just do this automatically instead of sending every Object through every filter or am I not thinking about CVS in the right way?

解决方案

You can do just this:

cvs.Filter += new FilterEventHandler(SomeFilterFunction1);
cvs.Filter += new FilterEventHandler(SomeFilterFunction2);

public void SomeFilterFunction1(object sender, FilterEventArgs e)
{
      SomeObject example = e.Item as SomeObject;
      e.Accepted &= example.Name.Contains("A");
      //if you prefer OR logic use this one:          
      //e.Accepted |= example.Name.Contains("A");

}

public void SomeFilterFunction2(object sender, FilterEventArgs e)
{
      SomeObject example = e.Item as SomeObject;
      e.Accepted &= example.Name.Contains("B");
      //if you prefer OR logic use this one:
      //e.Accepted |= example.Name.Contains("B");
}

What this will do is the do and AND (or OR if you use the |= operator) with the current value in the Accepted property, making your filters work together.

EDIT: This was made in .NET 4.5

这篇关于如何在一个集合视图源上一个接一个地应用多个过滤函数(AND关系)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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