dotnet / dotnet/wpf

StackPanel in ScrollViewer : invalid visual descendant bounds after adding items (making vertical scrollbar appear) then removing items

Open
#9,077 6 comments 1 reaction 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

StackPanel in ScrollViewer : when adding items to StackPanel (to make vertical scrollbar appear than removing them), than removing some of them (which makes scrollbar disppear), VisualTreeHelper.GetDescendantBounds() provides an incorrect value, which corresponds to the vertical scrollbar height before removing items.

=> Several possible side effects. In my case, an OpacityMask was stretched.

Note : VisualTreeHelper.GetContentBounds is correct though.

### Reproduction Steps

I have a StackPanel in a scrollviewer.

I add items to this StackPanel so that vertical scrollbar appears, than I remove some them (to make scrollbar disappear).
I do it programmatically (stackPanel.Children.Add/Remove).

VisualTreeHelper.GetDescendantBounds then gives an incorrect height for my stackPanel, which corresponds to the scrollbar height before I remove some items.

If I spy the track control inside the scrollbar, its actual height is the invalid one.

### Expected behavior

VisualTreeHelper.GetDescendantBounds() should provide the real height

### Actual behavior

VisualTreeHelper.GetDescendantBounds() provide the bad height (see above).

### Regression?

_No response_

### Known Workarounds

My issue was related to OpacityMask => I've forced the mask ViewPort height from my ScrollViewer ActualHeight.

### Impact

_No response_

### Configuration

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.