gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:49:39 25 4
gpt4 key购买 nike

我有一个绑定(bind)到 CollectionViewSource 的 ListView。我按照这篇文章(被很多人引用)进行多重过滤:http://www.zagstudio.com/blog/456#.UG8r6E1lWLE

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

这里是相关的代码:(背景:这个应用程序处理为运输软件过滤“订单”)

加载订单:

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

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

最佳答案

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

  1. 选中 checkBox1,然后 filterString 将为“Standard”并连接 shippingMethodFilter。
  2. 选中 checkBox2,然后 filterString 将为“TX”并连接 billingStateFilter。

shippingMethodFilter 在任何时候都不会取消 Hook 。所以它会继续基于“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;
}

关于WPF 多个过滤器 CollectionViewSource,添加的第一个过滤器工作正常,添加的第二个过滤器没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12752591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com