2

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.

Community
  • 1
  • 1
onmyway133
  • 45,645
  • 31
  • 257
  • 263

1 Answers1

2

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.

Michiel
  • 468
  • 3
  • 23
  • you're right. I follow the article you point, but when I set **** but it doesnot work – onmyway133 Dec 21 '12 at 10:57
  • yes, here is the XAML http://pastebin.com/6BSfeyqc and here is the code http://pastebin.com/yxwcFbGX – onmyway133 Dec 21 '12 at 11:24
  • Your advice about naming the ItemsPresenter may not have helped the poster, but helped it me, so thank you, thank you!!! Before naming, I could not get virtualizing an ItemsControl to work correctly, even though my ControlTemplate is the very basic format. After naming the ItemsPresenter, it worked like a charm! – grimus Feb 07 '13 at 05:17