WPF ComboBox 首次下拉显示慢的问题

69

由于Item项多且每项显示内容多(ItemTemplate复杂的话也会导致慢,比如要显示image之类的),导致使用ComboBox显示列表异常缓慢,这是由于ComboBox在展开列表时绘制了列表中的每一项,为了避免这个问题,我们可以使用VirtualizingStackPanel来代替ComboBox默认的项目面板StackPanel。

VirtualizingStackPanel 类的作用是开启虚拟化技术, 延迟那些看不见的子元素的绘制与渲染

<ComboBox x:Name="shopComBox" Width="350" Height="25" ItemsSource="{Binding ShopList}" SelectedValue="{Binding SelectShop}">
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
    <ComboBox.ItemTemplate>
        <DataTemplate DataType="{x:Type entitys:XbMaster}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding SerialNo}"/>
                <TextBlock Text="|" Margin="4,0,4,0"/>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>