WPF多个过滤器CollectionViewSource,第一个过滤器添加正常,第二个过滤器添加没有

时间:2012-10-05 19:09:51

标签: wpf filter event-handling collectionviewsource

我有一个限制在CollectionViewSource的ListView。我按照这篇文章(许多人的意思)进行了多重过滤:http://www.zagstudio.com/blog/456#.UG8r6E1lWLE

我设置了两个用于测试的复选框,除了添加过滤器之外什么都不做。每当我首先点击任何一个,过滤器就会被添加到CollectionViewSource并且它可以工作。然后,当我点击相反的复选框时,而不是将其他过滤器添加到CollectionViewSource并且两个过滤器都工作,我的列表视图变为空白(当它不应该基于数据时,这会以检查我的复选框的顺序发生)

以下是相关代码:(背景:此应用程序处理过滤软件的“订单”)

加载订单:

public class Order
{
    public int index { get; set; }
    public string host { get; set; }
    public Int64 orderNumber { get; set; }
    public string batchStatus { get; set; }
    public string sku { get; set; }
    public int numItems { get; set; }
    public string orderSource { get; set; }
    public string sourceOrderNumber { get; set; }
    public DateTime orderDate { get; set; }
    public DateTime orderTime { get; set; }
    public int customerID { get; set; }
    public string shipMethod { get; set; }
    public string billingState { get; set; }
    public bool statusChanged { get; set; }
    public int numSkus { get; set; }
    public string marketName { get; set; }
    public float weight { get; set; }
}

public class Orders : ObservableCollection<Order>
{
    public Orders()
    {
        SqlDataReader reader1 = cmd.ExecuteReader();
        while (reader1.Read())
        {
            Order order = new Order();

            order.host = (string)safeGetString(reader1, 0);
            order.orderNumber = (Int64)reader1["OrderNumber"];
            order.batchStatus = (string)safeGetString(reader1, 2);
            order.orderSource = (string)safeGetString(reader1, 3);
            order.sourceOrderNumber = safeGetString(reader1, 4);
            order.orderDate = (DateTime)reader1["OrderDate"];
            order.customerID = (int)reader1["CustomerID"];
            order.shipMethod = (string)safeGetString(reader1, 7);
            order.billingState = (string)safeGetString(reader1, 8);
            order.numItems = (int)reader1["NumItems"];
            order.numSkus = (int)reader1["NumSKUs"];
            order.marketName = (string)safeGetString(reader1, 11);
            order.weight = (float)(double)reader1["ShippedWeight"];


            this.Add(order);
        }
        reader1.Close();
    }

设置CollectionViewSource:

cvs = (CollectionViewSource)(this.Resources["cvs"]);

复选框功能:(硬编码“使用filterString进行过滤的内容”)

public void checkBox2_Checked(object sender, RoutedEventArgs e)
    {
        filterString = "TX";
        cvs.Filter += new FilterEventHandler(billingStateFilter);
    }
    public void checkBox1_Checked(object sender, RoutedEventArgs e)
    {
        filterString = "Standard";
        cvs.Filter += new FilterEventHandler(shippingMethodFilter);
    }

最后,过滤器:

private void shippingMethodFilter(object sender, FilterEventArgs e)
    {
        Order order = e.Item as Order;
        if ((order.shipMethod != filterString))
        {
            e.Accepted = false;
        }
    }

    public void billingStateFilter(object sender, FilterEventArgs e)
    {
        Order order = e.Item as Order;
        if ((order.billingState != filterString))
        {
            e.Accepted = false;
        }
    }

就像我说的那样,第一个过滤器始终有效。第二个总是使屏幕空白。有什么想法吗?

3 个答案:

答案 0 :(得分:1)

您正在为两个过滤器重复使用过滤器字符串,一旦您选中每个框,将应用两个过滤器。所以如果你:

  1. 检查checkBox1,然后filterString将为“Standard”,并且将连接shippingMethodFilter。
  2. 检查checkBox2,然后filterString将为“TX”,并且将连接billingStateFilter。
  3. 在任何时候,shippingMethodFilter都没有取消。因此它将继续根据“TX”的filterString进行过滤。

    您应该添加一个过滤器方法,检查是否选中了checkBox1 / checkBox2,然后可选地应用它的过滤逻辑。类似的东西:

    private string shippingFilterString = "Shipping";
    private string billingFilterString = "TX";
    
    private void collFilter(object sender, FilterEventArgs e)
    {
        Order order = e.Item as Order;
        if (checkBox1.IsChecked == true && (order.shipMethod != shippingFilterString ))
            e.Accepted = false;
        if (checkBox2.IsChecked == true && (order.billingState != billingFilterString ))
            e.Accepted = false;
    }
    

答案 1 :(得分:0)

for (i = 0; i < numberItemsFilterStrings.Length; i++)
            {
                switch (numberItemsFilterStrings[i])
                {
                    case "One Item":
                        if (order.numItems != 1)
                        {
                            e.Accepted = false;
                        }
                        break;
                    case "Between 2 - 5":
                        if ((order.numItems < 2) || (order.numItems > 5))
                        {
                            e.Accepted = false;
                        }
                        break;
                    case "Between 6 - 25":
                        if ((order.numItems < 6) || (order.numItems > 24))
                        {
                            e.Accepted = false;
                        }
                        break;
                    case "Greater Than 25":
                        if (order.numItems < 25)
                        {
                            e.Accepted = false;
                        }
                        break;
                }
            }

所以这是我正在创建的其中一个过滤器的代码。它适用于第一个过滤器,因为不接受与过滤器不匹配的所有订单。但是当我检查第二个过滤器盒时,没有任何订单显示,因为最初接受的订单是真的,现在它们也不被接受而不留下任何订单

答案 2 :(得分:0)

我发布了一个博客:https://hoomanvisualstudio.blogspot.com/b/post-preview?token=mpPyAk0BAAA.ygnewdzslszANX8AchX0hg.uW17QZR22-f_JUqjDTBcyA&postId=4200491240358821673&type=POST

有很多方法。一种简单的方法是将条件转换为单个谓词。

MyViewModel myViewModel = new MyViewModel();

//将收集您的过滤器的集合 var _itemSourceList = myViewModel.MyCollection;

var filter = new Predicate(item =&gt;((Model)item).FirstName.ToString()。包含(&#34; John&#34;)&amp;&amp;((Model)item。).LastName。 。的ToString()包含(&#34; Doe的&#34));

_itemSourceList.Filter = filter;  myDataGrid.ItemsSource = myViewModel.MyCollection;