ListBox will not show when visibility change while SizeToContent is active
See original GitHub issueDescribe the bug
When a ListBox
is visible in a Window
with SizeToContent
enabled, if it’s hidden and reshown again it will not show.
To Reproduce
Create new Avalonia app with the following code -
MainWindow.axaml
<Window xmlns="https://github.com/avaloniaui"
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"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Avalonia.ListBoxRepro.MainWindow"
SizeToContent="Height"
Title="Avalonia.ListBoxRepro">
<StackPanel>
<StackPanel Orientation="Horizontal">
<Button Click="Hide">Hide</Button>
<Button Click="Show">Show</Button>
</StackPanel>
<ListBox Name="ListBox">
<ListBoxItem>Item 1</ListBoxItem>
<ListBoxItem>Item 2</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 4</ListBoxItem>
</ListBox>
</StackPanel>
</Window>
MainWindow.axaml.cs
private void Hide(object? sender, RoutedEventArgs e)
{
this.FindControl<ListBox>("ListBox").IsVisible = false;
}
private void Show(object? sender, RoutedEventArgs e)
{
this.FindControl<ListBox>("ListBox").IsVisible = true;
}
Click hide, then click show, the list box will not show again.
Expected behavior ListBox (or any VirtualizingStackPanel usage) to show in this scenario.
Additional context
This seems to be due to VirtualizingStackPanel
’s MeasureOverride
not having any space to work with and then not generating any elements. I believe this should stay like this, but MeasureOverride
should trigger again when the control becomes visible.
I can confirm it by manually calling this.FindControl<ListBox>("ListBox").ItemsPanelRoot.InvalidateMeasure()
after making it visible.
Not sure if it makes sense, but doing this change solves the issue - https://github.com/AvaloniaUI/Avalonia/blob/ee62816405916c1d8014b1121e1c206151e4c804/src/Avalonia.Controls/VirtualizingStackPanel.cs#L132-L135 to -
protected override Size MeasureOverride(Size availableSize)
{
if (!IsEffectivelyVisible)
{
InvalidateMeasure();
return default;
}
Issue Analytics
- State:
- Created 6 months ago
- Comments:10 (10 by maintainers)
Top GitHub Comments
Great! I’ll close this issue when that PR gets merged then.
@adirh3 for new features, yes. Bugfixes are still okay 😉