WPF ComboBox performance problems by binding a large collections

前端 未结 3 1740
無奈伤痛
無奈伤痛 2020-12-04 09:24

I\'m trying to bind a large collection to a ComboBox and I faced performance problems when opening ComboBox\'s popup. I searched internet and found that using VirtualizingSt

相关标签:
3条回答
  • 2020-12-04 09:47

    I had the issue with slow performance as well. But I had created a class that inherited form Combobox, therefor I would like to do this programmatically. So here is that solution for other googlers out there.

    ItemsPanel = new ItemsPanelTemplate();
    var stackPanelTemplate = new FrameworkElementFactory(typeof (VirtualizingStackPanel));
    ItemsPanel.VisualTree = stackPanelTemplate;
    
    0 讨论(0)
  • 2020-12-04 09:56

    According to this blog: http://vbcity.com/blogs/xtab/archive/2009/12/15/wpf-using-a-virtualizingstackpanel-to-improve-combobox-performance.aspx

    I've tested it with this code:

    <ComboBox Name="cbBlah" ItemsSource="{Binding}">
        <ComboBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel />
            </ItemsPanelTemplate>
        </ComboBox.ItemsPanel>
    </ComboBox>
    

    It works fine for first time and next times. It's not necessary to code these lines:

    <VirtualizingStackPanel VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" />
    

    I hope this helps you.

    0 讨论(0)
  • 2020-12-04 10:00

    I just ran into this issue as well. I'm using this code in a custom combo box with a style template. When I ran my code in VS debugging mode the virtualization did not work properly. Once I ran it outside of debugging I can switch the content of the ObservableCollection without locking the UI up. It also might help if you set a max height and max width.

    <Setter Property="ScrollViewer.CanContentScroll" Value="True"/> 
    <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
    <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
    
    <Popup>
        <Border/>
        <ScrollViewer>
          <VirtualizingStackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/>
        </ScrollViewer> 
      </Grid>
    </Popup>
    
    0 讨论(0)
提交回复
热议问题