我有一个用于显示多种类型(从相同的基本类型的所有派生),它们通过的ObservableCollection&LT绑定的ItemsSource的项目列表框; T>
。
I've got a ListBox that is used to show items of multiple types (all derived from the same base type) that are bound to the ItemsSource through an ObservableCollection<T>
.
的性能表示列表框是可怕的。看来,虚拟化是禁用的。据: http://msdn.microsoft.com /en-us/library/cc716879(v=vs.110).aspx 似乎将多种类型的项目到ItemsControl的可能是问题。
The performance of said ListBox is terrible. It appears that virtualization is disabled. According to: http://msdn.microsoft.com/en-us/library/cc716879(v=vs.110).aspx it seems that adding items of multiple types to the ItemsControl may be the problem.
下面是我的ListBox的风格:
Here's my ListBox's style:
<Style x:Key="{x:Type ListBox}" TargetType="{x:Type ListBox}">
<Setter Property="ScrollViewer.CanContentScroll" Value="true" />
<Setter Property="VirtualizingPanel.VirtualizationMode" Value="Recycling" />
<Setter Property="VirtualizingPanel.IsVirtualizing" Value="True" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Visible" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Grid Grid.IsSharedSizeScope="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentPresenter Content="{StaticResource ListHeader}" />
<ScrollViewer Grid.Row="1" Margin="0" Focusable="False">
<VirtualizingStackPanel Margin="2" IsItemsHost="True" />
</ScrollViewer>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
有在绑定的ObservableCollection&LT的DataTemplates为每个可能的类型; T&GT;
,他们都是从同一个基类派生的所有类。一个例子的DataTemplate是:
There are DataTemplates for each of the possible types in the bound ObservableCollection<T>
and they are all classes that derive from the same base class. An example DataTemplate is:
<DataTemplate DataType="{x:Type ma:TimerEvent}">
<Grid Background="{StaticResource TargetCalledBackgroundColor}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="ShotNumberColumn" />
<ColumnDefinition Width="Auto" SharedSizeGroup="ShotTimeColumn" />
<ColumnDefinition Width="Auto" SharedSizeGroup="SplitTimeColumn" />
<ColumnDefinition Width="Auto" SharedSizeGroup="TargetNumberColumn" />
<ColumnDefinition Width="Auto" SharedSizeGroup="TotalTimeColumn" />
<ColumnDefinition Width="Auto" SharedSizeGroup="ScoreColumn" />
</Grid.ColumnDefinitions>
<Image Source="/LASR;component/Assets/Announcement.png" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBlock Grid.Column="1" Grid.ColumnSpan="2" Foreground="{StaticResource TargetCalledForegroundColor}" Text="{Binding DisplayText}" TextAlignment="Center" />
<TextBlock Grid.Column="3" Foreground="{StaticResource TargetCalledForegroundColor}" Text="{Binding TargetNumberCalled}" TextAlignment="Center" />
<TextBlock Grid.Column="4" Foreground="{StaticResource TargetCalledForegroundColor}" Text="{Binding Path=TotalTime.TotalSeconds, StringFormat={}{0:0.00}}" TextAlignment="Center" />
</Grid>
</DataTemplate>
列表框XAML是在这里:
ListBox XAML is here:
<UserControl>
<Grid>
<ListBox ItemsSource="{Binding}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
</UserControl>
WPF窗口的层次是:
The WPF window's hierarchy is:
<Window>
<Grid>
<Grid Name="MainWindowContent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="340*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<GroupBox Grid.Row="1" Grid.Column="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<UserControl />
</Grid>
</GroupBox>
</Grid>
</Grid>
</Window>
有没有办法解决这个问题,或者任何人都可以看到任何其他原因,虚拟化可能会关闭?
Is there a solution to this problem or can anyone see any other reason virtualization may be turned off?
您可以下载实例项目。
You can download an example project here.
感谢。
在您的样本项目的绩效是大幅通过删除 ColumnDefinitions
与改进 SharedSizeGroups
:
Performance in your sample project is drastically improved by removing the ColumnDefinitions
with SharedSizeGroups
:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="ShotNumberColumn" />
<ColumnDefinition Width="Auto" SharedSizeGroup="ShotTimeColumn" />
<ColumnDefinition Width="Auto" SharedSizeGroup="SplitTimeColumn" />
<ColumnDefinition Width="Auto" SharedSizeGroup="TargetNumberColumn" />
<ColumnDefinition Width="Auto" SharedSizeGroup="TotalTimeColumn" />
<ColumnDefinition Width="Auto" SharedSizeGroup="ScoreColumn" />
</Grid.ColumnDefinitions>
需要 SharedSizeGroup
的计算是太重了,应该尽量避免,如果你有大量的项目。
The calculations needed for SharedSizeGroup
are too heavy and should be avoided if you have large number of items.
使用一个的ListView
代替。