【发布时间】:2018-11-23 15:16:50
【问题描述】:
我有一个带有文本框的用户控件和一个自定义列表控件,它基本上是一个带有 CollectionView 的 ListBox。我想使用 CollectionView 的过滤功能,并使用文本框中的文本来过滤可见元素。
xaml 的简化表示:
<TextBox x:Name="FilterTextControl"/>
<CustomControls:OverviewControl
x:Name="ProfileOverviewControl"
FilterText="{Binding ElementName=FilterTextControl, Path=Text, Mode=OneWay, Delay=5000}"
Items="{Binding AllItems}"/>
CollectionViewSource:
<CollectionViewSource x:Key="GroupedProfiles"
Source="{Binding Items, RelativeSource={RelativeSource AncestorType=local:OverviewControl}}"
Filter="GroupedProfiles_OnFilter">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="Location" />
<componentModel:SortDescription PropertyName="Description" />
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Location" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
OverviewControl 中的FilterText 依赖属性:
public string FilterText
{
get => (string)GetValue(FilterTextProperty);
set => SetValue(FilterTextProperty, value);
}
public static readonly DependencyProperty FilterTextProperty =
DependencyProperty.Register(nameof(FilterText), typeof(string),
typeof(ProfileOverviewControl), new FrameworkPropertyMetadata(OnFilterTextChanged));
private static void OnFilterTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var intanceOfThisClass = (ProfileOverviewControl)d;
if (_collectionViewSource == null)
_collectionViewSource = intanceOfThisClass.FindResource("GroupedProfiles") as CollectionViewSource;
_collectionViewSource?.View?.Refresh();
}
OnFilterEvent 方法:
private void GroupedProfiles_OnFilter(object sender, FilterEventArgs e)
{
e.Accepted = string.IsNullOrEmpty(FilterText) || e.Item.ToString().Contains(FilterText);
}
问题
正如您在 FilterText 的绑定中看到的那样,我有 5000 毫秒的延迟。出于测试目的,我将其设置为 5000 毫秒而不是合理的 500 毫秒。 为了使过滤器工作,我需要刷新 CollectionView。 但是,PropertyChangedCallback 会在每次更改后立即触发,而不是使用延迟绑定来限制它。
我不太了解这种行为。如果这正是延迟绑定的工作方式,是否有其他方法可以限制 CollectionView 刷新?
【问题讨论】: