2011-06-01 77 views
1

我需要圍繞由項目控件填充的虛擬化堆疊面板的滾動查看器。顯然,高度是無限的,這就是爲什麼它沒有虛擬化。爲什麼?更多虛擬化堆棧面板高度/慢度問題

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <ScrollViewer> 
     <ItemsControl ItemsSource="{Binding InterestingHosts}"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <VirtualizingStackPanel IsItemsHost="True" /> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Vertical"> 
         <TextBlock FontSize="14"><TextBlock Text="{Binding Host.HostName}" FontWeight="Bold" FontSize="18" /> (<TextBlock Text="{Binding Host.IPAddress}" />)</TextBlock> 
         <StackPanel> 
          <StackPanel.Style> 
           <Style TargetType="StackPanel"> 
            <Setter Property="Margin" Value="10" /> 
            <Style.Triggers> 
             <DataTrigger Binding="{Binding LoggedOnUsers.Count}" Value="0"> 
              <Setter Property="Visibility" Value="Collapsed" /> 
             </DataTrigger> 
            </Style.Triggers> 
           </Style> 
          </StackPanel.Style> 
          <TextBlock Text="The follow domain and/or enterprise admins are logged on to the host: "/> 
          <DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding LoggedOnUsers}"> 
           <DataGrid.Columns> 
            <DataGridTextColumn Width="200" Binding="{Binding Domain}" Header="Domain"/> 
            <DataGridTextColumn Width="200" Binding="{Binding Name}" Header="Username"/> 
           </DataGrid.Columns> 
          </DataGrid> 
         </StackPanel> 
         <StackPanel> 
          <StackPanel.Style> 
           <Style TargetType="StackPanel"> 
            <Setter Property="Margin" Value="10" /> 
            <Style.Triggers> 
             <DataTrigger Binding="{Binding Processes.Count}" Value="0"> 
              <Setter Property="Visibility" Value="Collapsed" /> 
             </DataTrigger> 
            </Style.Triggers> 
           </Style> 
          </StackPanel.Style> 
          <TextBlock Text="The follow domain and/or enterprise admins are running processes on the host: "/> 
          <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Processes}" IsReadOnly="True"> 
           <DataGrid.Columns> 
            <DataGridTextColumn Width="200" Header="Name" IsReadOnly="False" Binding="{Binding Name}"/> 
            <DataGridTextColumn Width="200" Binding="{Binding Username}" Header="Username" /> 
            <DataGridTextColumn Binding="{Binding ID}" Header="ID" /> 
           </DataGrid.Columns> 
          </DataGrid> 
         </StackPanel> 
         <StackPanel> 
          <StackPanel.Style> 
           <Style TargetType="StackPanel"> 
            <Setter Property="Margin" Value="10" /> 
            <Style.Triggers> 
             <DataTrigger Binding="{Binding Services.Count}" Value="0"> 
              <Setter Property="Visibility" Value="Collapsed" /> 
             </DataTrigger> 
            </Style.Triggers> 
           </Style> 
          </StackPanel.Style> 
          <TextBlock Text="The follow domain and/or enterprise admins are authenticated for services on the host: "/> 
          <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Services}" IsReadOnly="True"> 
           <DataGrid.Columns> 
            <DataGridTextColumn Header="Name" Binding="{Binding Name}" /> 
            <DataGridTextColumn Header="Username" Binding="{Binding Username}" /> 
            <DataGridTextColumn Header="Start Mode" Binding="{Binding StartMode}" /> 
            <DataGridTextColumn Header="State" Binding="{Binding State}" /> 
           </DataGrid.Columns> 
          </DataGrid> 
         </StackPanel> 
        </StackPanel> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </ScrollViewer> 
</Grid> 

回答

3

看看this answer還有更多讓一個ItemsControl虛擬化不僅僅是設置VirtualizingStackPanel

基本上,把ScrollViewer中你的ItemsControl模板您ItemsPresenter周圍,並設置您的ItemsControl上的和ScrollViewer.CanContentScroll="True"

1

VirtualizingStackPanel需要在ScrollViewer中的直接孩子。你可以看到它實現了IScrollInfo,這是它與父ScrollViewer的關係。

因此,在ListBox的情況下,控件模板中已經有一個ScrollViewer(它最終將成爲VirtualizingStackPanel的父級)ItemsControl的默認ControlTemplate沒有ScrollViewer,因此您需要添加它有什麼樣:

<Style x:Key="{x:Type ItemsControl}" 
     TargetType="{x:Type ItemsControl}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ItemsControl}"> 
       <Border Background="{TemplateBinding Background}" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}" 
         Padding="{TemplateBinding Padding}" 
         SnapsToDevicePixels="true"> 
        <ScrollViewer CanContentScroll="True"> 
         <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        </ScrollViewer> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style>