我有一个绑定到 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;
}
}
就像我说的,第一个过滤器总是有效的。第二个总是使屏幕空白。有任何想法吗?