Skip to content

ItemContainerStyle is not always applied for ItemsControl #10786

Open

Description

@kazo0

Current behavior

If the Setter for ControlTemplate of an ItemsControl is set before the Setter for ItemContainerStyle, the ItemContainerStyle will never be set when adding items to the ItemsControl directly in the XAML.

Expected behavior

Windows:
window

How to reproduce it (as minimally and precisely as possible)

ItemContainerStyleBug.zip
Sample repro app attached above

Define a base style for a custom ItemsControl:

<Style x:Key="MyBaseItemsControlStyle"
		TargetType="local:MyItemsControl">
	<Setter Property="Template">
		<Setter.Value>
			<ControlTemplate TargetType="local:MyItemsControl">
				<Grid>
					<ItemsPresenter />
				</Grid>
			</ControlTemplate>
		</Setter.Value>
	</Setter>
</Style>

Define an ItemsControl style based on the one you just created:

<Style x:Key="MyBlueItemsControlStyle"
		BasedOn="{StaticResource MyBaseItemsControlStyle}"
		TargetType="local:MyItemsControl">
	<Setter Property="ItemContainerStyle"
			Value="{StaticResource MyBlueItemsControlItemStyle}" />
</Style>

Define a style for the custom ItemsControl's Item:

<Style x:Key="MyBlueItemsControlItemStyle"
		TargetType="local:MyItem">
	<Setter Property="Background"
			Value="Blue" />
	<Setter Property="BorderBrush"
			Value="CornflowerBlue" />
	<Setter Property="BorderThickness"
			Value="5" />
	<Setter Property="Template">
		<Setter.Value>
			<ControlTemplate TargetType="local:MyItem">
				<Grid Background="{TemplateBinding Background}"
						BorderBrush="{TemplateBinding BorderBrush}"
						BorderThickness="{TemplateBinding BorderThickness}">
					<ContentPresenter Content="{TemplateBinding Content}" />
				</Grid>
			</ControlTemplate>
		</Setter.Value>
	</Setter>
</Style>

In your MainPage, set the Page content to:

<Grid Margin="0,100,0,0">
	<StackPanel Spacing="20">
		<TextBlock Text="Using ItemsSource" />
		<local:MyItemsControl Style="{StaticResource MyBlueItemsControlStyle}"
								ItemsSource="{x:Bind Items}" />
		<TextBlock Text="Directly added in XAML" />
		<local:MyItemsControl Style="{StaticResource MyBlueItemsControlStyle}">
			<local:MyItem Content="1" />
			<local:MyItem Content="2" />
			<local:MyItem Content="3" />
			<local:MyItem Content="4" />
			<local:MyItem Content="5" />
		</local:MyItemsControl>
	</StackPanel>
</Grid>

Notice that the first ItemsControl is displaying the Items properly, but the second ItemsControl is not applying the MyBlueItemsControlItemStyle

ios

Workaround

Define the ItemContainerStyle and the Template within the same Style, ensuring that the ItemContainerStyle Setter appears BEFORE the Template Setter

Works on UWP/WinUI

Yes

Environment

Uno.WinUI / Uno.WinUI.WebAssembly / Uno.WinUI.Skia

NuGet package version(s)

No response

Affected platforms

WebAssembly, Android, iOS, macOS (AppKit), Mac Catalyst, Skia (WPF), Skia (GTK on Linux/macOS/Windows), Skia (Linux Framebuffer), Skia (Tizen)

IDE

Visual Studio 2022

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

No response

Activity

  1. added
    kind/bug
    triage/untriaged
    difficulty/tbd
    on Dec 20, 2022
  2. added
    project/layout 🧱
    on Jan 9, 2023
  3. changed the title [-]ItemContainerStyle is not always applied for ItemsControl[/-] [+]`ItemContainerStyle` is not always applied for `ItemsControl`[/+] on Aug 23, 2023
  4. added
    project/styling 👔
    area/listview 📃
    project/items 🧾
    difficulty/medium 🤔
    and removed
    triage/untriaged
    difficulty/tbd
    on Aug 23, 2023
  5. github-actions commented on Aug 7, 2025

    @github-actions
    Contributor

    Hey there — just a heads-up that this issue has been quiet for over a year. If there’s no update or comment in the next 10 days, it’ll be closed automatically. Feel free to remove the stale label or drop a note to keep it open.

  6. added
    stale
    on Aug 7, 2025
  7. removed
    stale
    on Aug 7, 2025
  8. MartinZikmund commented on Aug 7, 2025

    @MartinZikmund
    Member

    Still relevant

  9. added
    triage/potentially-fixed
    on Apr 8, 2026
  10. MartinZikmund commented on Apr 8, 2026

    @MartinZikmund
    Member

    Repro generator

    Runtime test(s) added in branch dev/mazi/repro/10786:

    • src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_ListViewBase.cs → When_ItemContainerStyle_Set_In_BasedOn_Style_After_Template

    ⚠️ The test(s) pass on current master. The test creates an ItemsControl with a BasedOn style where the Template Setter is defined in the base style and the ItemContainerStyle Setter in the derived style, with items added directly in XAML. The ItemContainerStyle is correctly applied to containers on Skia Desktop. The issue may have been fixed since it was reported (December 2022).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/listview 📃difficulty/medium 🤔has-repro-branchkind/bugproject/items 🧾project/layout 🧱project/styling 👔triage/potentially-fixed

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions