Inside an ItemsControl or ListView, is it possible to make the height of the GroupStyle.HeaderTemplate stretch to match the height of the grouped items?
Regardless of what I set VerticalAlignment/VerticalContentAlignment to, the group header appears to the left of the first item from the group.
A group header is actually a
ListViewHeaderItemobject. By looking through theViewTreewe will not see a parent container for every group (one group contains oneListViewHeaderItemand severalListViewItems.). So it seems like there is no parent control forListViewHeaderItemto simply strength. But we can set the height ofListViewHeaderItemto march the group items by accurate calculation.Here we can use the ViewTreeHelper class to get the
AcualHeightofListViewItemfirstly, and then get the items count for current group from the group resource you bind to XAML, and now the group header height can be calculated. Code as follows:XAML Code
Code behind
Update the HeaderContainerStyle for group style to make the layout look tidy.
And the result.