I read that when styling incorrectly, ListBox
will lose its virtualization. As notacat's answer in this question, his solution is OK.
I realize that whenever I put ItemsPresenter in another container, such as Grid
, Border
, ...
<ControlTemplate TargetType="ListBox">
<ScrollViewer x:Name="ScrollViewer"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Foreground="{TemplateBinding Foreground}"
Padding="{TemplateBinding Padding}">
<StackPanel>
<TextBlock Text="{TemplateBinding MyText}" />
<ItemsPresenter/>
</StackPanel>
</ScrollViewer>
</ControlTemplate>
ListBox
will lose its virtualization right away. But I do want to place a TextBlock
before ItemsPresenter
, and ScrollViewer only accepts 1 child element.
How to deal with this ?
UPDATE:
I see this post, which Derek Lakin does is to place that TextBlock
to the ScrollViewer
template, but how can place that TextBlock
in ScrollContentPresenter
?
P/S: I do set VirtualizingStackPanel
as ItemsPanel
for my ListBox
.
You're creating an itemtemplate for a listbox, but it is important for the ItemPresenter to be the direct child of the scrollviewer. Check out http://msdn.microsoft.com/en-us/library/ms754242.aspx for more information on how to template the listbox. I also found this article really helpfull.
If the itempresenter can not be direct child, you must give the ItemsPresenter the name, ItemsPresenter.