Null Reference Exception in VirtualizingStackPanel.cs
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
### Description
Crash happens if you put VirtualizingStackPanel inside any VirtualizingPanel, e.g. VirtualizingStackPanel.
[VitrualizingStackPanel.zip](https://github.com/user-attachments/files/18747321/VitrualizingStackPanel.zip)
### Reproduction Steps
Put ItemsControl with VirtualizingStackPanel iside other VirtualizingStackPanel and add new Item to it at runtime.
```
```
Code behind is:
```
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ItemsControl.ItemsSource = Items;
}
private void OnButtonClick(object sender, RoutedEventArgs e) => Items.Insert(0,"Hello World");
public ObservableCollection Items { get; } = new ();
}
```
The problem could be reproduced on .net6-9. I haven't tested earlier versions though.
Crash happens inside VirtualizingStackPanel.cs
```
protected override bool ShouldItemsChangeAffectLayoutCore(bool areItemChangesLocal, ItemsChangedEventArgs args)
{
\\...
if (IsScrolling)
{
flag = !IsExtendedViewportFull();
if (!flag)
{
UpdateExtent(areItemChangesLocal);
}
}
else
{
DependencyObject itemsOwnerInternal = ItemsControl.GetItemsOwnerInternal(this);
if (VisualTreeHelper.GetParent(itemsOwnerInternal) is VirtualizingPanel virtualizingPanel)
{
UpdateExtent(areItemChangesLocal);
IItemContainerGenerator itemContainerGenerator = virtualizingPanel.ItemContainerGenerator;
// Here itemContainerGenerator is null, but is not checked for null, as a result NullReferenceException is thrown
int itemIndex = ((ItemContainerGenerator)itemContainerGenerator).IndexFromContainer(itemsOwnerInternal, returnLocalIndex: true);
ItemsChangedEventArgs args2 = new ItemsChangedEventArgs(NotifyCollectionChangedAction.Reset, itemContainerGenerator.GeneratorPositionFromIndex(itemIndex), 1, 1);
flag = virtualizingPanel.ShouldItemsChangeAffectLayout(areItemChangesLocal: false, args2);
}
else
{
flag = true;
}
}
}
```
### Expected behavior
No Exception is thrown
### Actual behavior
Exception is thrown.
### Regression?
It seems it worked on .net framework 4.8, but doesn't work for .net6-9.
### Known Workarounds
Put non-virtualizing panel in-between ItemsControl and VirtualizingStackPanel:
```
```
### Impact
_No response_
### Configuration
_No response_
### Other information
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.