C# 为什么列表框项目在筛选期间不更新?

C# 为什么列表框项目在筛选期间不更新?,c#,.net,wpf,data-binding,filter,C#,.net,Wpf,Data Binding,Filter,我正在写一个程序来查看列表框中的产品信息。我有一个用于搜索的文本框,可以在您按ProductName键入时自动筛选列表。我已经运行了很多次我的C代码,我可以看到过滤器实际工作,但我不能直观地让它过滤或“刷新”屏幕 C代码: private ICollectionView _ProductInfoView; public ICollectionView ProductInfoView { get{return this._ProductInfoView;}

我正在写一个程序来查看列表框中的产品信息。我有一个用于搜索的文本框,可以在您按ProductName键入时自动筛选列表。我已经运行了很多次我的C代码,我可以看到过滤器实际工作,但我不能直观地让它过滤或“刷新”屏幕

C代码:

private ICollectionView _ProductInfoView;
    public ICollectionView ProductInfoView 
    {
        get{return this._ProductInfoView;}
        set
        {
            this._ProductInfoView=value;
            this.onPropertyChnage("ProductInfoView");
        }
    }

 private void RibbonSetupProduct_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        this.hidePanels();
        new Task(() =>
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                ObservableCollection<ModelProductInformation> productInfoCollection   = new ObservableCollection<ModelProductInformation>(from ProductInfo in new GTS_ERPEntities().ProductInformations select new ModelProductInformation { ProductID = ProductInfo.ProductID,  ProductName = ProductInfo.ProductName , Remark=ProductInfo.Remark});
                this.ProductInfoView = CollectionViewSource.GetDefaultView(productInfoCollection);
                new ProductInfoSearch(ProductInfoView, this.TestTextBox);
            }
                ), DispatcherPriority.DataBind);
        }
        ).Start(); 

        this.PanelProducts.Visibility = Visibility.Visible;
    }

  class ProductInfoSearch
   {
      public ProductInfoSearch(ICollectionView filteredList, TextBox textEdit)
      {
        string filterText = string.Empty;

        filteredList.Filter = delegate(object obj)
        {
            if (String.IsNullOrEmpty(filterText))
            {
                return true;
            }
            ModelProductInformation str = obj as ModelProductInformation;
            if (str.ProductName==null)
            {
                return true;
            }
            if (str.ProductName.ToUpper().Contains(filterText.ToUpper()))
            {
                return true;
            }
            else
            {
                return false;
            }
        };
        textEdit.TextChanged += delegate
        {
            filterText = textEdit.Text;
            filteredList.Refresh();
        };
    }
}
XAML:

<dxe:ListBoxEdit x:Name="ProductInfoList" Margin="1.666,1,8,8" Grid.Column="2" Grid.Row="2" Grid.RowSpan="5"  DisplayMember="ProductName" DataContext="{Binding ProductInfoView, ElementName=window}" ItemsSource="{Binding}"/>

我想我的问题是数据绑定或内部任务。

我会将ObservableCollection设置为私有字段,只创建一次实例,也只创建一次ICollectionView。要添加新数据,您可以在您的收藏中使用clear and add-试试看,它对我有用

 private ObservableCollection<ModelProductInformation> productInfoCollection;

 //ctor, just once in the constructor
 this.productInfoCollection =  new ObservableCollection<ModelProductInformation>();
 this.ProductInfoView = CollectionViewSource.GetDefaultView(productInfoCollection);
 new ProductInfoSearch(ProductInfoView, this.TestTextBox);


private void RibbonSetupProduct_Click(object sender, System.Windows.RoutedEventArgs e)
{
    this.hidePanels();
    new Task(() =>
    {
        this.Dispatcher.Invoke(new Action(() =>
        {
            var yourData = from ProductInfo in new GTS_ERPEntities().ProductInformations select new ModelProductInformation { ProductID = ProductInfo.ProductID,  ProductName = ProductInfo.ProductName , Remark=ProductInfo.Remark};
            //if you wanna change the collection, simple clear and add(or create AddRange extension)
           this.productInfoCollection.Clear();

           foreach(var data in yourData)
           { this.productInfoCollection.Add(data);}
        }
            ), DispatcherPriority.DataBind);
    }
    ).Start(); 

    this.PanelProducts.Visibility = Visibility.Visible;
}

我删除了任务内容,但它仍然不起作用。请确定是否有其他解决方案。是否尝试过实例化一次并将productInfoCollection作为类成员?我过去在使用ICollection的局部变量时遇到了一些问题,就像您在ProductInfoCollection中遇到的一样。我的问题已经解决了。问题在列表框中。我使用了Developer Express ListBoxEdit。这不是工作。我不知道为什么。现在我使用列表框,工作很好,你的答案也是正确的。非常感谢。