我正在使用 .NET 4.5/VS2012,我有一个 ListView 看起来像这样
<ListView
VirtualizingPanel.IsContainerVirtualizable="True"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.IsVirtualizingWhenGrouping="True"
Grid.Row="1"
Name="eventLogList"
Margin="5,0,5,0"
BorderBrush="Black"
BorderThickness="2"
ItemsSource="{Binding EventLogs}"
SelectedItem="{Binding SelectedEventLog}"
local:ListViewSorter.CustomListViewSorter="EventLogViewer.UI.EventLogItemComparer"
SelectionMode="Single">
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="False">
<GroupStyle.ContainerStyle>
<Style TargetType="GroupItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GroupItem">
<Expander IsExpanded="True">
<Expander.Header>
<TextBlock FontSize="20" TextWrapping="Wrap" Margin="0,10,0,5" >
<Bold><TextBlock Text="{Binding Name}"/></Bold> - <TextBlock FontSize="20" Text="{Binding ItemCount}"/> logs
</TextBlock>
</Expander.Header>
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
<ListView.View>
<GridView>
<GridViewColumn
Header="event id"
Width="120"
DisplayMemberBinding="{Binding EventID}" />
<GridViewColumn
Header="level"
Width="160"
DisplayMemberBinding="{Binding Level}" />
<GridViewColumn
Header="server"
Width="160"
DisplayMemberBinding="{Binding Server}" />
<GridViewColumn
Header="log name"
Width="160"
DisplayMemberBinding="{Binding LogName}" />
<GridViewColumn
Header="source"
Width="240"
DisplayMemberBinding="{Binding Source}" />
<GridViewColumn
Header="logged"
Width="240"
DisplayMemberBinding="{Binding Logged}" />
</GridView>
</ListView.View>
</ListView>
但是性能仍然没有改善。我找到了一个使用 ListBox 的示例,但是如何虚拟化 ListView?我很挣扎。我听说在以前版本的 WPF 中,通过分组,虚拟化是关闭的,但是在 .NET 4.5 中,WPF 有一个IsVirtualizingWhenGrouping
属性,我已经将它设置为True
.
更新:罪魁祸首是自定义样式,删除它后,列表视图运行流畅如黄油