6

How to stretch listview items width to fill parent container?

The default behaviour seems to be squishing everything as narrow as possible.

  • 1
    possible duplicate of [ListViewItem won't stretch to the width of a ListView](http://stackoverflow.com/questions/15067309/listviewitem-wont-stretch-to-the-width-of-a-listview) – yasen Jun 25 '14 at 11:07
  • 2
    I don't normally downvote questions, but it seems you posted this just to share the answer. This is generally fine, but in this case, the first result from google when searching for this problem is a question in SO from one year ago that's already answered, and it has the actual code! – yasen Jun 25 '14 at 11:12

1 Answers1

26

The problem is not with the child item but with the itemcontainer which has a HorizontalAlignment=Left hidden somewhere deep inside the framework code. Only after fixing this will you get the behaviour one would expect to be there by default. Before and after screenshots showing both the problem and the solution

<ListView.ItemContainerStyle>
  <Style TargetType="ListViewItem">
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
  </Style>
</ListView.ItemContainerStyle>

<GroupStyle.HeaderContainerStyle >
  <Style TargetType="ListViewHeaderItem">
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
  </Style>
</GroupStyle.HeaderContainerStyle>
Michael Mairegger
  • 6,833
  • 28
  • 41
  • 2
    An image - great! Only one thing is missing from this answer, but it's not all that important... Yeah, I'm talking about the actual code that solves the problem... – yasen Jun 25 '14 at 11:05