Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListBox with Virtualization Causes InvalidOperationException and UI Freezing in MaterialDesignThemes 5.2.0 #3790

Open
keeleycenc opened this issue Feb 11, 2025 · 0 comments
Labels
bug evaluation required Items is pending review or evaluation by the team

Comments

@keeleycenc
Copy link

Bug explanation

Bug Report: Issue with MaterialDesignThemes Version 5.2.0

Environment:

  • MaterialDesignThemes Version: 5.2.0
  • WPF: .NET 8.0
  • OS: Windows 11 Professional 23H2 22631.4751
  • CPU: Intel(R) Core(TM) i7-14700KF 3.40 GHz
  • RAM: 32.0 GB

Description

After upgrading to MaterialDesignThemes version 5.2.0 from 5.1.0, the following issues have occurred:

  1. UI Freezing/Blocking: The application experiences a brief UI freezing when auto-scrolling and removing items from the ListBox that uses virtualization.
  2. System.InvalidOperationException: The following exception occurs intermittently when auto-scrolling a ListBox:
System.InvalidOperationException: "Cannot call StartAt when content generation is in progress."

Steps to Reproduce

  1. Upgrade MaterialDesignThemes to version 5.2.0.
  2. Use a ListBox with virtualization enabled (e.g., VirtualizingStackPanel.VirtualizationMode="Recycling").
  3. Bind a collection (such as InstantMessage) to the ItemsSource of the ListBox.
  4. Implement auto-scrolling when new messages are added (e.g., via ScrollIntoView in the ViewModel_OnMessageUpdated method).
  5. Remove items from the ListBox (e.g., by updating the collection).
  6. Experience brief UI freezing and occasional System.InvalidOperationException.

Code Example

<ListBox
 Height="300"                    
 x:Name="MyInstantMessageListBox"
 ItemContainerStyle="{StaticResource CustomListBoxItemStyle}"
 Template="{StaticResource CustomListBoxTemplate}"
 Grid.Row="1" Grid.ColumnSpan="2"
 Background="Transparent"
 ItemsSource="{Binding InstantMessage}"
 VirtualizingStackPanel.IsVirtualizing="True"
 VirtualizingStackPanel.VirtualizationMode="Recycling">
 <ListBox.ItemsPanel>
     <ItemsPanelTemplate>
         <VirtualizingStackPanel />
     </ItemsPanelTemplate>
 </ListBox.ItemsPanel>
 <ListBox.ItemTemplate>
     <DataTemplate>
         <StackPanel 
             Orientation="Horizontal" 
             VerticalAlignment="Center">
             <TextBlock 
                 Foreground="{DynamicResource TextFillColorPrimaryBrush}"
                 Text="{Binding Timestamp, StringFormat='HH:mm:ss.fff'}" 
                 VerticalAlignment="Center" Width="95"/>
             <md:PackIcon
                 Margin="0, 0, 5, 0"
                 Kind="{Binding Level, Converter={StaticResource InfoLevelToIconKindConverter}}" 
                 Foreground="{Binding Level, Converter={StaticResource InfoLevelToIconColorConverter}}"
                 VerticalAlignment="Center"/>
             <TextBox 
                 Text="{Binding Message}" VerticalAlignment="Center"
                 IsReadOnly="True" BorderThickness="0" Background="Transparent" TextWrapping="Wrap"
                 TextAlignment="Left" HorizontalAlignment="Stretch"/>
         </StackPanel>
     </DataTemplate>
 </ListBox.ItemTemplate>
 <ListBox.Resources>
     <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
             <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesign2.Defaults.xaml" />
         </ResourceDictionary.MergedDictionaries>
         <Style TargetType="ListBox">
             <Setter Property="BorderBrush" Value="{DynamicResource MasBorderBrush}"/>
             <Setter Property="BorderThickness" Value="0,0,0,1"/>
         </Style>
     </ResourceDictionary>
 </ListBox.Resources>
</ListBox>
private void ViewModel_OnMessageUpdated(object? sender, EventArgs e) {
    try {
        if (_isMsgAutoScroll && InstantMessageListBox.Items.Count > 0) {
            InstantMessageListBox.ScrollIntoView(InstantMessageListBox.Items[^1]);
        }
    } catch {
        // BUG: ListBox virtualization mode + auto-scrolling error: System.InvalidOperationException:“Cannot call StartAt when content generation is in progress.”
    }
}

Expected Behavior

The application should smoothly auto-scroll the ListBox when new items are added, even if items are removed simultaneously, without causing UI freezing or throwing exceptions.

Actual Behavior

  • UI Freezing/Blocking: The UI experiences brief freezing when auto-scrolling and removing items from the ListBox with virtualization enabled.
  • Exception: The InvalidOperationException is thrown intermittently with the message: "Cannot call StartAt when content generation is in progress."

Additional Notes

  • The issue only occurs after upgrading to version 5.2.0.
  • The problem does not occur in version 5.1.0, which was working fine with the same code and configuration.
  • Using delay scheduling does not resolve the issue.
Application.Current.Dispatcher.BeginInvoke(new Action(() => {
    InstantMessageListBox.ScrollIntoView(InstantMessageListBox.Items[^1]);
}), DispatcherPriority.Background);
  • After removing the MaterialDesign2 resource from ListBox.Resources, the issue disappears, Remove the following code:
<ListBox.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesign2.Defaults.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <Style TargetType="ListBox">
            <Setter Property="BorderBrush" Value="{DynamicResource MasBorderBrush}"/>
            <Setter Property="BorderThickness" Value="0,0,0,1"/>
        </Style>
    </ResourceDictionary>
</ListBox.Resources>
  • Queue Updates Every Second: The queue updates every second, with a maximum of 15 items per cycle. The performance is not an issue, and this points to a potential issue with MaterialDesignThemes, rather than the performance of the application.

Screenshot

Image

Version

5.2.0

@keeleycenc keeleycenc added bug evaluation required Items is pending review or evaluation by the team labels Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug evaluation required Items is pending review or evaluation by the team
Projects
None yet
Development

No branches or pull requests

1 participant