我如何明确地调用类似“DoFilter”的东西System.Windows.Controls.ItemCollection
?
我将它的过滤器属性设置为谓词。我在谓词中放置了一个断点,它仅在初始化 ItemsCollection 时到达那里,当我调用 m_ItemsCollection.Refresh() 时它不是。
我如何明确地调用类似“DoFilter”的东西System.Windows.Controls.ItemCollection
?
我将它的过滤器属性设置为谓词。我在谓词中放置了一个断点,它仅在初始化 ItemsCollection 时到达那里,当我调用 m_ItemsCollection.Refresh() 时它不是。
有几种情况 .Refresh() 不起作用,但这确实有效:
collection.Filter = collection.Filter;
几个月前我遇到了这个问题。显然,有一个错误使 ItemsControl 在某些情况下无法可靠地传递 Refresh() 调用。我没有调查细节。
刷新有时不起作用的原因是因为在 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 方法实际上并没有调用 do any to _collectionView
,所以 refresh 什么也不做!
/// <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();
}
很抱歉回答老问题,但觉得值得澄清。