У меня есть пользовательский элемент управления с текстовым полем и пользовательский элемент управления списком, который в основном представляет собой ListBox с CollectionView.Я хотел бы использовать функцию фильтрации 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>
Свойство зависимостей FilterText в OverviewControl:
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?