dotnet / dotnet/wpf

GroupStyle cause ListBox to use the wrong ItemPanel

Open
#7,828 3 comments 0 reactions 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

When adding item to a **empty** `ListBox`, if a `GroupStyle` is defined, `ListBox` will use the GroupStyle's `ItemPanelTemplate` instead of its own. It's important to note that no grouping or sorting is applied to the `ListBox`.

### Reproduction Steps

Build and Run the Project
[DBSolution.zip](https://github.com/dotnet/wpf/files/11471012/DBSolution.zip)
Right Click to open the context menu. Choose "Add"
Take a look in the Visual Tree.

### Expected behavior

WrapPanel is used.
![image](https://github.com/dotnet/wpf/assets/52592373/bbf367e4-b84f-4b55-804c-eae6ed31c23b)

### Actual behavior

But StackPanel is used instead
![image](https://github.com/dotnet/wpf/assets/52592373/0a7615e7-6cd5-4c56-abb1-3cfc4bece01f)

### Regression?

_No response_

### Known Workarounds

- The ListBox is not empty
If the ItemsSource provided to the ListBox already have item inside.
- Reinitalize the ListBox after adding items
In the project provided, add some items to the ListBox then switch to a different tab and switch back.

The ListBox will be reintialize and use the correct PanelTemplate in these cases.

### Impact

_No response_

### Configuration

.NET 7.0 both Debug and Release Build
Windows 10 Home 19045.2965
x64
Do you know whether it is specific to that configuration? **No**

### Other information

Refereshing the Collection via `CollectionViewSource.GetDefaultView()` doesn't seen to affect anthing

My original [reddit post](https://www.reddit.com/r/csharp/comments/13ae6u6/help_listbox_applying_the_wrong_itempaneltemplate) about the issue. Someone suggested making a bug report

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.