Repository navigation
Description
Current behavior
Severity: Low - setting or swapping ItemContainerStyle (or ItemContainerStyleSelector) on a plain ItemsControl after its containers exist is silently ignored, so runtime style swaps don't show up until the containers are recreated.
On a plain ItemsControl (ContentPresenter containers), assigning ItemContainerStyle after the first layout leaves the already-realized containers unstyled. ListView/GridView are not affected because the ListViewBase family overrides Refresh().
Expected behavior
The style is re-applied to the realized containers. In WinUI, ItemsControl::OnItemContainerStyleChangedImpl calls RefreshContainers() (dxaml/xcp/dxaml/lib/ItemsControl_Partial.cpp, also OnItemContainerStyleSelectorChangedImpl). A native WinUI 3 run (WinAppSDK 2.5.1) of the repro below gives container Opacity 0.5 and 0.5. Uno gives 1 and 1.
How to reproduce it (as minimally and precisely as possible)
- Host an
ItemsControlwith two items and wait for layout. - Assign an
ItemContainerStylewith a setter targetingContentPresenter. - Read the
Opacityof the containers.
var ic = new ItemsControl { ItemsSource = new[] { "a", "b" } };
Window.Content = ic;
// wait for layout / loaded
ic.ItemContainerStyle = (Style)XamlReader.Load(
"<Style xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' TargetType='ContentPresenter'>" +
"<Setter Property='Opacity' Value='0.5'/></Style>");
// await idle
var c0 = (ContentPresenter)ic.ContainerFromIndex(0);
var c1 = (ContentPresenter)ic.ContainerFromIndex(1);
// WinUI: c0.Opacity == 0.5, c1.Opacity == 0.5
// Uno: c0.Opacity == 1, c1.Opacity == 1Setting the style before the first layout works on both.
Runtime-verified: a runtime test with exactly this repro fails on Skia desktop (Windows) on master 46ea872727d with Expected:<0.5>. Actual:<1>.
Workaround
Reset ItemsSource after changing the style so the containers are recreated.
Works on UWP/WinUI
Yes (container Opacity 0.5 on WinUI 3, WinAppSDK 2.5.1).
Environment
master 46ea872727d, net11.0-desktop, .NET SDK 11.0.100
NuGet package version(s) affected
master 46ea872727d
Affected platforms
All (shared ItemsControl code); runtime-verified on Skia desktop (Windows).
IDE
n/a
Anything else we need to know?
Root cause (source-backed): OnItemContainerStyleChanged only calls Refresh(), and Refresh() is an empty private protected virtual in ItemsControl. Only the ListViewBase family overrides it. ApplyItemContainerStyle only runs when a container is prepared.
- https://github.com/unoplatform/uno/blob/46ea872727d/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs#L335 (
OnItemContainerStyleChanged => Refresh()) - https://github.com/unoplatform/uno/blob/46ea872727d/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs#L351 (
OnItemContainerStyleSelectorChanged => Refresh()) - https://github.com/unoplatform/uno/blob/46ea872727d/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs#L1670 (empty
Refresh()) - https://github.com/unoplatform/uno/blob/46ea872727d/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs#L1683 (
ApplyItemContainerStyle)
A possible fix is to re-run ApplyItemContainerStyle over the realized containers when the style or selector changes (WinUI skips the selector refresh when ItemContainerStyle is set).
Related but different: #10786 is about the order of setters inside a Style, not about changing the style at runtime.
Activity