我编写了一个带搜索扩展功能的自定义 WPF 控件,我们将其称为 MyControl
。
该控件是 ItemsControl
类的子类。
因此,我像这样向其提供数据源:
控件本身使用
protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
{
if (newValue != null)
{
ICollectionView view = CollectionViewSource.GetDefaultView(newValue);
view.Filter += this.FilterPredicate;
}
if (oldValue != null)
{
ICollectionView view = CollectionViewSource.GetDefaultView(oldValue);
view.Filter -= this.FilterPredicate;
}
base.OnItemsSourceChanged(oldValue, newValue);
}
过滤源集合的视图(从而在内部ListBox中显示它)。
现在假设我们在XAML中定义了10个这些具有相同DynamicSource的MyControl。 问题是,如果其中一个对源集合应用了过滤器,则也会影响所有其他实例。
您将如何更改控件以避免出现此行为?