question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ListBox will not show when visibility change while SizeToContent is active

See original GitHub issue

Describe 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:closed
  • Created 6 months ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
grokyscommented, Apr 27, 2023

Edit - Seems like https://github.com/AvaloniaUI/Avalonia/pull/11127 fixes the issue in the repro.

Great! I’ll close this issue when that PR gets merged then.

1reaction
timuniecommented, Mar 28, 2023

@adirh3 for new features, yes. Bugfixes are still okay 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

SizeToContent with a Listbox - wpf
I notice that without the SizeToContent="WidthAndHeight" I can easily get the Listbox to limit itself and use a scrollbar. However, this has the ......
Read more >
Listbox visible false
I am trying to use code to hide a listbox. This is what I have so far, but it's not working. I know...
Read more >
How to change listbox item size dynamically in C# WPF?
I'll take a stab at what you want to do. You're trying to dynamically change the size of an image from a selection...
Read more >
Excel VBA Userform ListBox '.Visible=True' does not cancel ...
Visible =False' Office 365. I am facing an issue that I cannot figure out why my ListBox will not show up in the...
Read more >
UI for WPF 2011.31116
Fixed: Changing the effective ElementType at run-time while the grid is grouped, filtered or sorted results in an InvalidCastException. Fixed: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found