ICollection过滤器错误转换类型

时间:2015-10-22 08:24:11

标签: c# wpf mvvm

我已过滤我的收藏

这是我的代码

 public ICollectionView LogEntriesStoreView { get; set; }

  var collection = new ObservableCollection<Service>(this._model.GetService());

   this.LogEntriesStoreView = CollectionViewSource.GetDefaultView(collection);

            this.LogEntriesStoreView.Filter += new FilterEventHandler(ShowOnlyBargainsFilter);


private void ShowOnlyBargainsFilter(object sender, FilterEventArgs e)
        {
            AuctionItem product = e.Item as AuctionItem;
            if (product != null)
            {
                // Filter out products with price 25 or above
                if (product.CurrentPrice < 25)
                {
                    e.Accepted = true;
                }
                else
                {
                    e.Accepted = false;
                }
            }
        }

现在我收到了这个错误 隐式类型转换&#34; System.Windows.Data.Filter EventHandler&#34;在&#34; System.Predicate&#34;不能

this._view = new TViewType();
            this._model = new ServiceModel();
            this.Service = new ObservableCollection<Service>(this._model.GetService());
            this.OkCommand = new RelayCommand(o => this.OKRun());
            this.LostFocusCommand = new RelayCommand(o => this.LostFocusOKRun());

            // в переменную получаем нашу коллекцию
            var collection = new ObservableCollection<Service>(this._model.GetService());
            // инициализируем поле типа ICollectionView нашей коллецией
            this.LogEntriesStoreView = CollectionViewSource.GetDefaultView(collection);

            this.LogEntriesStoreView.Filter = new Predicate<object>(Contains);

            this.LogEntriesStoreView.Filter += new FilterEventHandler(ShowOnlyBargainsFilter);


            this._view.SetDataContext(this);
            this._view.ShowIView();
        }

        private void OKRun()
        {
            MessageBox.Show("One Click");
        }

        private void LostFocusOKRun()
        {
            MessageBox.Show("LostFocus");
        }

        private void TextChanged()
        {

        }

        private void ShowOnlyBargainsFilter(object sender, FilterEventArgs e)
        {

        }

        public bool Contains(object de)
        {

            return true;
        }

完整的代码,我在我的代码中添加了一个谓词。所有这些都是以msdn

为例

1 个答案:

答案 0 :(得分:0)

EventHandler应该适用于CollectionViewSource LogEntriesStoreView

Collection for CollectionView和CollectionViewSource的实现之间存在差异。

https://msdn.microsoft.com/en-us/library/system.windows.data.collectionview.filter(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource.filter(v=vs.110).aspx