Note that there are some explanatory texts on larger screens.

plurals
  1. POPerformance degredation with custom ListBox ControlTemplate
    text
    copied!<p>Using a template for a custom control deriving from <code>ListBox</code> causes filtering of <code>ItemSource</code> to become slow. The filtering is done in the get of the <code>ItemSource</code> that the control is bound to. This problem is not present when a normal <code>ListBox</code> is used, so why should it be any different for a custom <code>ListBox</code>?</p> <p>Filtering:</p> <pre><code>public IEnumerable&lt;LibraryViewModel&gt; Libraries { get { if (!string.IsNullOrEmpty(this.LibrarySearchString)) return _libraries.Where(lib =&gt; IsLibraryMatch(lib, this.LibrarySearchString)); else return _libraries.OrderBy(lib =&gt; !lib.IsFavourite); } } </code></pre> <p>Using the control:</p> <pre><code>&lt;con:FilterListBox Grid.Row="1" ItemsSource="{Binding Libraries}" SelectedItem="{Binding SelectedLibrary}" ItemTemplate="{StaticResource LibraryItemTemplate}" SearchString="{Binding LibrarySearchString, Mode=TwoWay}" IsSearching="False" Margin="4"/&gt; </code></pre> <p>The control template:</p> <pre><code>&lt;Style x:Key="{x:Type con:FilterListBox}" TargetType="{x:Type con:FilterListBox}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type con:FilterListBox}"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition/&gt; &lt;/Grid.RowDefinitions&gt; &lt;DockPanel Grid.Row="0"&gt; &lt;TextBlock Text="Search" VerticalAlignment="Center"/&gt; &lt;TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SearchString, UpdateSourceTrigger=PropertyChanged}" Margin="4,0,0,0"/&gt; &lt;/DockPanel&gt; &lt;ScrollViewer Grid.Row="1" CanContentScroll="True"&gt; &lt;StackPanel IsItemsHost="True" HorizontalAlignment="Stretch"/&gt; &lt;/ScrollViewer&gt; &lt;TextBlock Grid.Row="1" Text="Searching..." HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSearching, Converter={StaticResource CollapsedIfFalseConverter}}"/&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>Thanks for any help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload