【问题标题】:enhance combobox expanding speed提高组合框扩展速度
【发布时间】:2012-01-22 02:02:38
【问题描述】:
我有一个包含 3000 个项目的组合框。展开它需要几秒钟。有没有办法更快地扩展它?使用 ItemsSource 和绑定路径绑定项目:
<ComboBox ItemsSource="{Binding Path=SomeItems}" />
【问题讨论】:
标签:
wpf
performance
user-interface
mvvm
combobox
【解决方案1】:
您必须使用虚拟化堆栈面板来提高性能。在这种情况下,只需将 ItemsPanel 的 ItemspanelTemplate 从 StackPanel 更改为 VirtualizingStackpanel 不会有任何魔力,因为当您按下向下按钮时,数据会加载到 Popup 中。因此您必须将 ScrollViewer 中的 StackPanel 修改为 VirtualizingStackpanel。为此,使用 Expression blend 或 VS 编辑组合框的 ControlTemplate/Style,并更改控件模板弹出区域,如下所示
<Popup
Name="Popup"
Placement="Bottom"
IsOpen="{TemplateBinding IsDropDownOpen}"
AllowsTransparency="True"
Focusable="False"
PopupAnimation="Slide">
<Grid
Name="DropDown"
MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border
x:Name="DropDownBorder"
Background="{StaticResource WindowBackgroundBrush}"
BorderThickness="1"
BorderBrush="{StaticResource SolidBorderBrush}"/>
<ScrollViewer Margin="4,6,4,6">
<VirtualizingStackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid> </Popup>
【解决方案2】:
<ComboBox>
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>