【问题标题】:Why List Box items are not update during Filtering?为什么列表框项目在筛选期间不更新?
【发布时间】:2012-05-02 04:18:40
【问题描述】:

我正在编写一个程序来查看列表框中的产品信息。我有一个用于搜索的文本框,它会在您按 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}"/>

我猜我的问题要么是数据绑定,要么是在 Task() 内部。

【问题讨论】:

    标签: c# .net wpf data-binding filter


    【解决方案1】:

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

     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。这不是工作。我不知道为什么。现在我正在使用 ListBox 并且工作正常,您的回答也是正确的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多