DependencyProperty的PropertyChangedCallback忽略延迟绑定



我有一个带有文本框的用户控件和一个自定义列表控件,它基本上是一个带有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的绑定中看到的,我有一个5000ms的延迟。出于测试目的,我将其设置为5000毫秒,而不是合理的500毫秒。为了使过滤器工作,我需要刷新CollectionView。但是,PropertyChangedCallback在每次更改后立即触发,而不是使用延迟绑定对其进行限制。

我不太理解这种行为。如果延迟绑定就是这样工作的,那么是否有其他方法可以限制CollectionView刷新?

尝试像这样反转绑定。通过这种方式,文本框更改将延迟。现在,延迟取决于过滤器属性的更改(如果从OverviewControl更改(。

<TextBox x:Name="FilterTextControl" Text="{Binding ElementName=ProfileOverviewControl, Path=FilterText, Delay=5000}"/>
<CustomControls:OverviewControl
x:Name="ProfileOverviewControl"
Items="{Binding AllItems}"/>

最新更新