Search code examples
wpflistviewvirtualizationitemspanel

How to make ListView change ItemControl to VirtualizingStackPanel at runtime


OK, so, I have a ListView-derived control that changes Grouping and ItemsSource on the fly. When I group such that the scrollbars dissapear, and then change my ItemsSource to a different ICollectionView, my scrollbars do not return.

The basic problem is that ListView changes to a VirtualizedStackPanel when grouping is activated and does not change back when grouping is de-activated.

I don't mind that virtualization is disabled when grouping--this is not a problem. What I need is a way to make the ListView regenerate it's ItemPanel when I change the ItemsSource.


Solution

  • Could you add an event handler to the SourceUpdated event and then reset the ItemsPanelTemplate to a template defined in your Resources?

    Something like:

    public MyWindow()
    {
       InitializeComponent();
    
       MyListView.SourceUpdated += new EventHandler<DataTransferEventArgs>( OnSourceUpdated );
    }
    
    void OnSourceUpdated( object sender, DataTransferEventArgs e )
    {
       MyListView.ItemsPanel = (ItemsPanelTemplate)Resources["MyItemsPanelTemplate"];
    }