Repository navigation
Description
Current behavior 🐛
When ItemsStackPanel is used as the ItemsPanel of a plain ItemsControl (not a ListView/GridView), the panel ends up with twice as many children as items: half are zero-sized, never-measured containers, the other half are real containers. In an app where the panel isn't given a fixed size (e.g. HorizontalAlignment="Right" in a horizontally unbounded parent) the panel measured to width 0 and nothing was drawn, with all children arranged at the same offset.
Observed with 6 items, ItemsControl of 300x300 (Skia Desktop):
panel 300x300; children: 0x0@0,0 | 0x0@0,0 | 0x0@0,0 | 0x0@0,0 | 0x0@0,0 | 0x0@0,0 | 9.3x19.3@0,0 | 9.3x19.3@0,0 | 9.3x19.3@0,0 | 10.7x19.3@0,0 | 8x19.3@0,0 | 7.3x19.3@0,0
Children.Count is 12 for 6 items, and every child is at offset 0,0 (the items are not stacked), for Orientation Horizontal and Vertical alike.
Expected behavior 🎯
One container per item, stacked along the panel orientation. WinUI supports ItemsStackPanel in a plain ItemsControl: its native test suite has ItemsStackPanelIntegrationTests::CanStackItemsHorizontally / CanStackItemsVertically, which use an ItemsControl with an ItemsStackPanel and expect items at (100 * i, 100) / (100, 100 * i).
How to reproduce it (as minimally and precisely as possible) 🔬
- Add this to a page:
and set
<ItemsControl x:Name="ic" Width="300" Height="300"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <ItemsStackPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl>
ic.ItemsSource = new[] { "A", "B", "C", "D", "E", "F" }; - Once loaded, find the
ItemsStackPanelin the visual tree. - Result:
panel.Children.Count == 12, children all atActualOffset == (0, 0). Expected: 6 children at increasing X.
Equivalent runtime test (Uno.UI.RuntimeTests):
[TestMethod]
[RunsOnUIThread]
public async Task When_ItemsStackPanel_Is_ItemsPanel_Of_ItemsControl()
{
var ic = new ItemsControl
{
Width = 300, Height = 300,
ItemsSource = new[] { "A", "B", "C", "D", "E", "F" },
ItemsPanel = (ItemsPanelTemplate)XamlReader.Load(
"<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><ItemsStackPanel Orientation='Horizontal'/></ItemsPanelTemplate>"),
};
WindowHelper.WindowContent = ic;
await WindowHelper.WaitForLoaded(ic);
await WindowHelper.WaitForIdle();
var panel = VisualTreeUtils.FindVisualChildByType<ItemsStackPanel>(ic);
Assert.AreEqual(6, panel.Children.Count); // actual: 12
}Confirmed at runtime on Skia Desktop (net11.0-desktop, master 4393a80). I first hit this in a real app (Uno.Sdk 7.0.0-dev.73) on desktop and WASM, where it showed up as a zero-width panel with nothing drawn; I did not re-run WASM on master.
Workaround 🛠️
Use a StackPanel (or VirtualizingStackPanel) as the ItemsPanel of a plain ItemsControl, or use a ListView if you need ItemsStackPanel.
Works on UWP/WinUI
Yes. Backed by the WinUI C++ sources (dxaml/test/native/external/controls/itemsstackpanel/ItemsStackPanelIntegrationTests.cpp, CanStackItemsHorizontally / CanStackItemsVertically), not by running a native WinUI repro.
Environment
Master 4393a80, Skia Desktop (net11.0-desktop). Initially seen on Uno.Sdk 7.0.0-dev.73, desktop and WASM (Skia).
Anything else?
Suspected root cause (code inspection, not proven by a fix):
ItemsStackPanelis anIVirtualizingPanelwhoseItemsStackPanelLayoutgenerates its own containers viaVirtualizingPanelGenerator(ItemsStackPanel.cs).ItemsControl.ShouldItemsControlManageChildrenisItemsPanelRoot == InternalItemsPanelRoot(ItemsControl.cs#L130), so a plainItemsControlalso adds its own containers into the panel.- Only
Selectoroverrides it to back off when the panel is anIVirtualizingPanel(Selector.managed.cs#L22). The two generators then both populate the sameChildrencollection.
This is a different problem from #25266 (GetItemsHostOrientations), which only concerns the orientation reported by Selector.
Activity