StackPanel vertical scrolling doesn't work properly when the Visibility of some of its children is Collapsed
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
### Description
The issue reproduces when you host a StackPanel (vertically oriented) in a ScrollViewer and the first few visual children of the StackPanel have their Visibility set to Collapsed. Also, the CanContentScroll of the ScrollViewer should be set to True.
[vide-stackpanel-scrolling-issue.webm](https://github.com/dotnet/wpf/assets/8738379/8f045bf6-1428-48af-ae58-f6f91c4dd51b)
In that case, it seems that the initial ViewportHeight of the StackPanel (coming from the IScrollInfo implementation) is calculated wrongly. When you start scrolling down, the ViewportHeight value starts updating until it reaching its actual value. Until then, the view is not scrolling at all. It feels like the you need to scroll out the invisible items from the viewport before the scrolling start working as expected.
The same behavior is observed also in the VirtualizedStackPanel. This means that you will automatically see it in the native ListBox, which has its ScrollViewer.CanContentScroll set to True and VirtualizedStackPanel as its default items panel.
This doesn't happen if CanContentScroll is False.
The issue seems related to the implementation of the ViewportHeight property of the StackPanel, and more specifically how the viewport height is calculated in the _scrollData.
### Reproduction Steps
The following XAML is enough to recreate the issue. You can also find a project attached to this report.
[stack-panel-scrolling-issue.zip](https://github.com/dotnet/wpf/files/13939985/stack-panel-scrolling-issue.zip)
```
```
### Expected behavior
The collapsed visuals should be ignored and the scrolling should work properly. In other words, it should start scrolling immediately from "Item 10" (see the attached project).
### Actual behavior
When scrolling, the vertical scrollbar thumb is moving and it should update the items in the viewport, but it doesn't until you virtually reach "Item 10" (see the attached project). In other words, you move the scrollbar's thumb, but nothing happens until you reach a specific vertical offset.
### Regression?
_No response_
### Known Workarounds
To work this around set the CanContentScroll of ScrollViewer to False.
### Impact
_No response_
### Configuration
This reproduces in both .NET Framework 4 and above, and in .NET Core 3.1 and later. It is reproducible in .NET 8 as well.
### Other information
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.