如何显式调用Items Collection.Filter?

时间:2010-02-12 03:54:48

标签: .net wpf filter itemscollection

如何在System.Windows.Controls.ItemCollection上明确调用类似'DoFilter'的内容?

我将它的Filter属性设置为Predicate。我在谓词中放置了一个断点,它只在ItemsCollection初始化时到达那里,当我调用m_ItemsCollection.Refresh()时它不是。

2 个答案:

答案 0 :(得分:2)

有几种情况,.Refresh()不起作用,但这样做:

collection.Filter = collection.Filter;
几个月前我遇到了这个问题。显然有一个错误会阻止ItemsControl在某些情况下可靠地传递Refresh()调用。我没有调查过细节。

答案 1 :(得分:0)

刷新有时不起作用的原因是因为在ItemsCollection上使用了这个代码:

   /// <summary>
    /// Set/get a filter callback to filter out items in collection.
    /// This property will always accept a filter, but the collection view for the
    /// underlying ItemsSource may not actually support filtering.
    /// Please check <seealso cref="CanFilter"/>
    /// </summary>
    /// <exception cref="NotSupportedException">
    /// Collections assigned to ItemsSource may not support filtering and could throw a NotSupportedException.
    /// Use <seealso cref="CanFilter"/> property to test if filtering is supported before assigning
    /// a non-null Filter value.
    /// </exception>
    public override Predicate<object> Filter
    {
        get
        {
            return (EnsureCollectionView()) ? _collectionView.Filter : MyFilter;
        }
        set
        {
            MyFilter = value;
            if (_collectionView != null)
                _collectionView.Filter = value;
        }
    } 

过滤器在底层集合视图上设置,而不是ItemsCollection本身。

然后基本的Refresh方法实际上并没有调用对_collectionView执行任何操作,因此刷新什么都不做!

/// <summary>
    /// Re-create the view, using any <seealso cref="SortDescriptions"/> and/or <seealso cref="Filter"/>.
    /// </summary>
    public virtual void Refresh()
    {
        IEditableCollectionView ecv = this as IEditableCollectionView;
        if (ecv != null && (ecv.IsAddingNew || ecv.IsEditingItem))
            throw new InvalidOperationException(SR.Get(SRID.MemberNotAllowedDuringAddOrEdit, "Refresh"));

        RefreshInternal();
    }  

很抱歉回答旧问题,但觉得值得澄清。