Windows Phone Style make ListBox ' s Virtualiz

2019-09-09 17:35发布

问题:

We know ListBox's ItemsPanelTemplate is VirtualizingStackPanel. I define a Style for my ListBox. The ListBox Virtualizing can't work. What can I do?

<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>

回答1:

try to change 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> 


回答2:

In your ControlTemplate change your <StackPanel> to a <VirtualizingStackPanel> and that should do it.