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 in StackPanel has wrong size

See original GitHub issue

When putting a ListBox into a StackPanel, the ListBox does not honor the vertical size restriction.

<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"
        d:DesignWidth="800" d:DesignHeight="450"
        x:Class="TopologyEditor.MainWindow"
        Title="MainWindow">
    <StackPanel VerticalAlignment="Stretch">
        <ListBox HorizontalAlignment="Left"
                 ScrollViewer.VerticalScrollBarVisibility="Visible" Items="{Binding Items}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Window>

In the example above the bottom part of the vertical scrollbar lies outside the window. I tried numerous VerticalAlignments and/or Components - I cannot manage to put a ListBox below any dynamically sized element.

This example produces an exception:

<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"
        d:DesignWidth="800" d:DesignHeight="450"
        x:Class="TopologyEditor.MainWindow"
        Title="MainWindow">
    <StackPanel VerticalAlignment="Stretch">
        <RelativePanel VerticalAlignment="Stretch">
            <ListBox RelativePanel.AlignBottomWithPanel="True" VerticalAlignment="Stretch" HorizontalAlignment="Left"
                     ScrollViewer.VerticalScrollBarVisibility="Visible" Items="{Binding Items}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </RelativePanel>
    </StackPanel>
</Window>

Unhandled exception. System.InvalidOperationException: Invalid size returned for Measure.
   at Avalonia.Layout.Layoutable.Measure(Size availableSize) in /_/src/Avalonia.Layout/Layoutable.cs:line 373
   at Avalonia.Controls.StackPanel.MeasureOverride(Size availableSize) in /_/src/Avalonia.Controls/StackPanel.cs:line 207
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize) in /_/src/Avalonia.Layout/Layoutable.cs:line 559
   at Avalonia.Layout.Layoutable.Measure(Size availableSize) in /_/src/Avalonia.Layout/Layoutable.cs:line 364
   at Avalonia.Layout.LayoutHelper.MeasureChild(ILayoutable control, Size availableSize, Thickness padding) in /_/src/Avalonia.Layout/LayoutHelper.cs:line 46
   at Avalonia.Layout.LayoutHelper.MeasureChild(ILayoutable control, Size availableSize, Thickness padding, Thickness borderThickness) in /_/src/Avalonia.Layout/LayoutHelper.cs:line 39
   at Avalonia.Controls.Presenters.ContentPresenter.MeasureOverride(Size availableSize) in /_/src/Avalonia.Controls/Presenters/ContentPresenter.cs:line 362
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize) in /_/src/Avalonia.Layout/Layoutable.cs:line 559
   at Avalonia.Layout.Layoutable.Measure(Size availableSize) in /_/src/Avalonia.Layout/Layoutable.cs:line 364
   at Avalonia.Layout.LayoutHelper.MeasureChild(ILayoutable control, Size availableSize, Thickness padding) in /_/src/Avalonia.Layout/LayoutHelper.cs:line 46
   at Avalonia.Controls.Primitives.VisualLayerManager.MeasureOverride(Size availableSize) in /_/src/Avalonia.Controls/Primitives/VisualLayerManager.cs:line 133
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize) in /_/src/Avalonia.Layout/Layoutable.cs:line 559
   at Avalonia.Layout.Layoutable.Measure(Size availableSize) in /_/src/Avalonia.Layout/Layoutable.cs:line 364
   at Avalonia.Layout.Layoutable.MeasureOverride(Size availableSize) in /_/src/Avalonia.Layout/Layoutable.cs:line 625
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize) in /_/src/Avalonia.Layout/Layoutable.cs:line 559
   at Avalonia.Layout.Layoutable.Measure(Size availableSize) in /_/src/Avalonia.Layout/Layoutable.cs:line 364
   at Avalonia.Layout.Layoutable.MeasureOverride(Size availableSize) in /_/src/Avalonia.Layout/Layoutable.cs:line 625
   at Avalonia.Controls.Window.MeasureOverride(Size availableSize) in /_/src/Avalonia.Controls/Window.cs:line 859
   at Avalonia.Controls.WindowBase.MeasureCore(Size availableSize) in /_/src/Avalonia.Controls/WindowBase.cs:line 246
   at Avalonia.Layout.Layoutable.Measure(Size availableSize) in /_/src/Avalonia.Layout/Layoutable.cs:line 364
   at Avalonia.Layout.LayoutManager.Measure(ILayoutable control) in /_/src/Avalonia.Layout/LayoutManager.cs:line 287
   at Avalonia.Layout.LayoutManager.ExecuteInitialLayoutPass() in /_/src/Avalonia.Layout/LayoutManager.cs:line 174
   at Avalonia.Controls.Window.ShowCore(Window parent) in /_/src/Avalonia.Controls/Window.cs:line 660
   at Avalonia.Controls.Window.Show() in /_/src/Avalonia.Controls/Window.cs:line 611
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 115
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime[T](T builder, String[] args, ShutdownMode shutdownMode) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 148

Avolonia 0.10.6

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
maxkatz6commented, Aug 2, 2021

In the example above the bottom part of the vertical scrollbar lies outside the window.

Expected. Stack Panel does not constraints child size, but allows to take as much space as it needs. Otherwise it wouldn’t be possible to “stack” elements after each other. Depending on your task you might need Grid or UniformGrid or DockPanel or else. Or you can remove scroll bar from ListBox by disabling it, and put ScrollViewer as parent of your StackPanel. It will work, but it will disable virtualization (if you have a lot of items there).

About second example, having ListBox inside of RelativePanel inside of StackPanel does not sounds as expected scenario… But exception also isn’t something that I might expect there.

0reactions
maxkatz6commented, Aug 6, 2021

I believe this issue can be closed. And for flickering you already created another one - https://github.com/AvaloniaUI/Avalonia/issues/6360

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bind the Height of the Listbox inside the StackPanel to ...
I want to bind the Height of the ListBox to the Height of the StackPanel so the ListBox stretches itself Vertically so the...
Read more >
Use the Right Container for ListBox Scrolling
What I learned is this: if you have an Auto sized ListBox, meaning that it will stretch its Width and Height to match...
Read more >
XAML StackPanel within DockPanel is not Docking Right
It works fine when HorizontalAlignment="Right" is set. Copy. <DockPanel LastChildFill="False"> < ...
Read more >
WPF XAML Listbox to display 1 Item from static Resource
I tried the following code but it displayed "Cinetique_GUI,Mode: instead of the Names from the Resource. What am I doing wrong?
Read more >
ListBox traps into infinite cycle on scroll · Issue #10643
ListBox needs to have a limited size to work correctly. So you shouldn't place it in a Stackpanel , as the StackPanel does...
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