Windows Phone风格使ListBox的虚拟化被禁用。如何恢复虚拟化?

时间:2012-01-12 18:34:09

标签: windows-phone-7

我们知道ListBox的ItemsPanelTemplate是VirtualizingStackPanel。我为ListBox定义了一个Style。 ListBox虚拟化无法工作。我该怎么办?

<Style x:Key="ListBoxStyle" 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>
                            <ItemsPresenter/>
                            <HyperlinkButton Content="Add More"
                                             FontSize="25"
                                             Grid.Row="1"
                                             Name="hybtnAddMerchant"
                                             Click="hybtnAddMerchant_Click"
                                             VerticalAlignment="Bottom"/>
                        </StackPanel>
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

2 个答案:

答案 0 :(得分:1)

尝试更改ControlTemplate:

<ControlTemplate TargetType="ListBox"> 
  <Grid>
     <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto"/>
     </Grid.RowDefinitions>
     <ScrollViewer x:Name="ScrollViewer" Grid.Row="0"
                   BorderBrush="{TemplateBinding BorderBrush}"  
                   BorderThickness="{TemplateBinding BorderThickness}"  
                   Background="{TemplateBinding Background}"  
                   Foreground="{TemplateBinding Foreground}"  
                   Padding="{TemplateBinding Padding}"> 
           <ItemsPresenter/> 
      </ScrollViewer> 
      <HyperlinkButton Content="Add More" FontSize="25" 
             Grid.Row="1" Name="hybtnAddMerchant" Click="hybtnAddMerchant_Click" VerticalAlignment="Bottom"/> 
  </Grid>
</ControlTemplate> 

答案 1 :(得分:-1)

在您的ControlTemplate中,将<StackPanel>更改为<VirtualizingStackPanel>,并且应该这样做。