ListBox
使用从导致过滤ItemSource
变慢的自定义控件的模板。过滤是在ItemSource
控件绑定到的 get 中完成的。使用normal 时不存在此问题ListBox
,那么为什么对于 custom 会有任何不同ListBox
?
过滤:
public IEnumerable<LibraryViewModel> Libraries {
get {
if (!string.IsNullOrEmpty(this.LibrarySearchString))
return _libraries.Where(lib => IsLibraryMatch(lib, this.LibrarySearchString));
else
return _libraries.OrderBy(lib => !lib.IsFavourite);
}
}
使用控件:
<con:FilterListBox Grid.Row="1"
ItemsSource="{Binding Libraries}"
SelectedItem="{Binding SelectedLibrary}"
ItemTemplate="{StaticResource
LibraryItemTemplate}"
SearchString="{Binding LibrarySearchString, Mode=TwoWay}"
IsSearching="False"
Margin="4"/>
控制模板:
<Style x:Key="{x:Type con:FilterListBox}" TargetType="{x:Type con:FilterListBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type con:FilterListBox}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<DockPanel Grid.Row="0">
<TextBlock Text="Search"
VerticalAlignment="Center"/>
<TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=SearchString,
UpdateSourceTrigger=PropertyChanged}"
Margin="4,0,0,0"/>
</DockPanel>
<ScrollViewer Grid.Row="1" CanContentScroll="True">
<StackPanel IsItemsHost="True"
HorizontalAlignment="Stretch"/>
</ScrollViewer>
<TextBlock Grid.Row="1"
Text="Searching..."
HorizontalAlignment="Center"
VerticalAlignment="Center"
Visibility="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=IsSearching,
Converter={StaticResource CollapsedIfFalseConverter}}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
谢谢你的帮助。