我想在我的 ListBoxes 中添加一个标题,我使用模板来做到这一点。问题是,如果我扩展 ListBox 的模板,似乎列表框的 virtualizingstackpanel 不再按预期工作:它会在我滚动之前加载所有内容。
我在 stackoverflow 中发现了一些类似的相关问题(VirtualizingStackPanel 在覆盖 ScrollViewer 的默认控件模板时停止工作),但是那里给出的解决方案不能应用于 WP7:我找不到滚动查看器的名为“CanContentScroll”的属性。
我的代码
<Style x:Key="ListBoxStyle1" TargetType="ListBox">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<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="..."/>
<ItemsPresenter/>
</StackPanel>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>