更多虚拟化堆栈面板高度/慢度问题

时间:2011-06-01 19:21:46

标签: wpf

我需要一个围绕由项目控件填充的虚拟化堆栈面板的滚动查看器。显然,高度是无限的,这就是为什么它不是虚拟化的原因。为什么呢?

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

2 个答案:

答案 0 :(得分:3)

看看this answer。制作ItemsControl虚拟化不仅仅是设置VirtualizingStackPanel

基本上,将ScrollViewer放在ItemsPresenter周围的ItemsControl模板中,并在ItemsControl上设置VirtualizingStackPanel.IsVirtualizing="True"ScrollViewer.CanContentScroll="True"

答案 1 :(得分:1)

VirtualizingStackPanel需要是ScrollViewer的直接子项。您可以看到它实现了IScrollInfo,这是将它与父ScrollViewer联系起来的。

所以在ListBox的情况下,控件模板中已经有一个ScrollViewer(最终将是VirtualizingStackPanel的父级.ProjectControl的默认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>