-
Notifications
You must be signed in to change notification settings - Fork 3.5k
ListBox with Virtualization Causes InvalidOperationException and UI Freezing in MaterialDesignThemes 5.2.0 #3790
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
Comments
@keeleycenc Thank you so much for the very detailed issue description!! Awesome work! However, even with all that in hand, I am unable to reproduce the issue 😢 I have created a standard WPF application ( I have tried 2 variations of the view model property "InstantMessages" ( I had to remove the Are you able to modify the code below to make it reproduce the issue you're facing? MainWindow.xaml <Window x:Class="Issue3790.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Issue3790" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
x:Name="_root"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ListBox Height="300"
VerticalAlignment="Center"
HorizontalAlignment="Center"
x:Name="MyInstantMessageListBox"
Grid.Row="1" Grid.ColumnSpan="2"
Background="Transparent"
ItemsSource="{Binding ElementName=_root, Path=InstantMessages2}"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center">
<TextBlock Foreground="Red"
Text="{Binding Timestamp, StringFormat='HH:mm:ss.fff'}"
VerticalAlignment="Center" Width="95"/>
<materialDesign:PackIcon Margin="0, 0, 5, 0"
Kind="Github"
Foreground="Blue"
VerticalAlignment="Center"/>
<TextBox Text="{Binding Message}"
VerticalAlignment="Center" Foreground="Red"
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>
<Button HorizontalAlignment="Left" VerticalAlignment="Bottom" Content="Remove First" Click="RemoveFirstButton_Click" />
<Button HorizontalAlignment="Right" VerticalAlignment="Bottom" Content="Remove Last" Click="RemoveLastButton_Click" />
</Grid>
</Window> MainWindow.xaml.cs using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
namespace Issue3790;
[ObservableObject]
public partial class MainWindow : Window
{
// "ViewModel" property using ObservableCollection<T>
public ObservableCollection<InstantMessage> InstantMessages { get; } = [];
// "ViewModel" property using plain List<T>
[ObservableProperty]
private List<InstantMessage> _instantMessages2 = [];
public MainWindow()
{
InitializeComponent();
Task.Run(() => AddMessages());
}
private async Task AddMessages()
{
while (true)
{
InstantMessage msg = new() { Timestamp = DateTime.Now, Message = "This is a message" };
// This line allocates a brand new list, and invokes INotifyPropertyChanged.PropertyChanged via the [ObservableProperty] codegen from CommunityToolkit.Mvvm
InstantMessages2 = new List<InstantMessage>([.. (InstantMessages2 ?? []), msg]);
await Dispatcher.BeginInvoke(() =>
{
// This line adds the message to the ObservableCollection (and thus needs to be executed on the UI thread)
//InstantMessages.Add(msg);
try
{
MyInstantMessageListBox.ScrollIntoView(MyInstantMessageListBox.Items[^1]);
}
catch
{
// BUG: ListBox virtualization mode + auto-scrolling error: System.InvalidOperationException:“Cannot call StartAt when content generation is in progress.”
}
});
await Task.Delay(50);
}
}
private void RemoveFirstButton_Click(object sender, RoutedEventArgs e)
{
if (InstantMessages.Count > 0)
InstantMessages.RemoveAt(0);
}
private void RemoveLastButton_Click(object sender, RoutedEventArgs e)
{
if (InstantMessages.Count > 0)
InstantMessages.RemoveAt(InstantMessages.Count - 1);
}
}
public class InstantMessage
{
public DateTime Timestamp { get; set; }
public string? Message { get; set; }
} |
This issue is marked stale because it has been open 30 days with no activity. Remove stale label or update the issue, otherwise it will be closed in 14 days. |
Bug explanation
Bug Report: Issue with MaterialDesignThemes Version 5.2.0
Environment:
Description
After upgrading to MaterialDesignThemes version 5.2.0 from 5.1.0, the following issues have occurred:
ListBox
that uses virtualization.ListBox
:System.InvalidOperationException: "Cannot call StartAt when content generation is in progress."
Steps to Reproduce
ListBox
with virtualization enabled (e.g.,VirtualizingStackPanel.VirtualizationMode="Recycling"
).InstantMessage
) to theItemsSource
of theListBox
.ScrollIntoView
in theViewModel_OnMessageUpdated
method).ListBox
(e.g., by updating the collection).System.InvalidOperationException
.Code Example
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
Additional Notes
MaterialDesign2
resource fromListBox.Resources
, the issue disappears, Remove the following code:Screenshot
Version
5.2.0
The text was updated successfully, but these errors were encountered: