Module 4 · Lesson 14 of 16
Ten thousand rows, thirty controls
A virtualizing panel creates containers for the viewport, not for the data. Count them instead of trusting the demo.
Most "Avalonia is slow" reports are one of three things: too many controls, too much layout, or work on the UI thread. This lesson is about the first one, because it is the easiest to measure.
Virtualization is the default — until it isn't
ListBox uses a virtualizing panel. It creates containers for the rows that fit on
screen, plus a small buffer, and recycles them as you scroll.
ItemsSource="{Binding Rows}"
Height="380"
BorderThickness="0"
Background="Transparent">
<ListBox.ItemTemplate>
<DataTemplate x:DataType="local:PerfRow">
<Grid ColumnDefinitions="72,*,90" Margin="2,2">
<TextBlock Text="{Binding Index}" Classes="caption" />
<TextBlock Grid.Column="1" Text="{Binding Name}" />
<TextBlock Grid.Column="2" Text="{Binding Bucket}" Classes="caption" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Border>
<StackPanel Spacing="6" MaxWidth="760" HorizontalAlignment="Left">
<TextBlock Classes="caption" TextWrapping="Wrap"
Text="The harness asserts this: 10,000 items in the source, fewer than 200 ListBoxItem containers in the visual tree. Replace the panel with a plain StackPanel and that number becomes 10,000." />
<TextBlock Classes="code" Text="<ListBox ItemsSource="{Binding Rows}" /> // virtualized by default" />
</StackPanel>
</StackPanel>
The data is not the cost. Ten thousand records are a few megabytes. Ten thousand
ListBoxItem controls, each with a template, a style lookup, and a layout pass, are
the cost.
The classic mistake is wrapping an
ItemsControlin aScrollViewer. The scroll viewer measures with infinite height, the panel decides every item fits, and virtualization quietly stops working. If a list is slow, count containers first.
How this page proves it
var realized = view.GetVisualDescendants().OfType<ListBoxItem>().Count();
if (realized > 200)
throw new InvalidOperationException($"virtualization failed: {realized} containers");
Ten thousand rows, fewer than two hundred containers. That assertion runs on every build, so a change that disables virtualization fails the course rather than shipping.
The other two levers
- Compiled bindings (default in 12) remove reflection from the binding path. A
{Binding}inside a row template runs for every realized row, every update. - Layout thrash comes from changing width, margin, or visibility in a loop. Measure once, mutate the model, and let one layout pass do the work.
Prefer ItemsControl when you do not need selection or scrolling — it is lighter than
ListBox. Prefer a plain TextBlock over a StackPanel of Runs. Prefer
StringFormat over a converter for display-only formatting.
Measure, do not guess
dotnet-counters monitor --process-id <pid> System.Runtime
A Stopwatch around the suspicious method is usually enough to prove where the time
goes. Screenshots and vibes are not measurements.
Try it
- Replace the
ListBoxwith anItemsControlinside aScrollViewer, run the verifier, and watch the container count explode. Revert. - Add a search box that filters the collection and confirm the assertion still passes with 10,000 items.
- Time how long the page takes to measure and arrange with 1,000 versus 10,000 rows. The difference should be nearly flat.
Next: proof, not vibes.
Files this lesson draws from
src/CourseApp/Lessons/16_Performance/PerfView.axamlsrc/CourseApp/Lessons/16_Performance/PerfViewModel.cs
VerifiedThe verifier hosts the list, asserts 10,000 source rows, and counts fewer than 200 realized row containers in the visual tree.