0

I`m writing a program to view product Information in a Listbox. I have a text box for searching that automatically filters the list as you typing by ProductName. I have run through my C# code numerous times and I can see the filter actually working but I cannot visually get it to filter or 'refresh' on the screen.

C# Code :

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}"/>

I guess my problem is either Data Binding or inside Task().

1 Answer 1

0

i would make the ObservableCollection a private field and just create the instance once and also just create the ICollectionView once. to add new data you can use clear and add on your Collection - try it, it works for me.

 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;
}
3
  • I remove Task stuff but it still not working.Please there is any other solution. May 2, 2012 at 7:09
  • did you try instancing once and productInfoCollection as class member? i had some issues in the past when using local variables with ICollection like you did with productInfoCollection
    – blindmeis
    May 2, 2012 at 7:36
  • I am solved my problem. The Problem was in List Box. I used Developer Express ListBoxEdit. It is not work. I don`t know why. Now I am use ListBox and work fine and your answer is also correct. Thank you. May 2, 2012 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.