0

リストボックスに製品情報を表示するプログラムを書いています。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() の内部にあると思います。

4

1 に答える 1

0

ObservableCollection をプライベート フィールドにして、インスタンスを 1 回だけ作成し、ICollectionView を 1 回だけ作成します。コレクションにクリアして追加できる新しいデータを追加するには、試してみてください。

 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;
}
于 2012-05-02T06:29:10.287 に答える