1

I am building a ListView that needs to have five columns - the first one needs to have text that can be any length and needs to wrap whenever the window size changes (in addition to changing the row height so the wrapped text is visible) and the other four columns are a static width of 45. I've been searching for hours on this and every solution I come across either requires a static width or doesn't work.

Solutions tried:

Column widths of auto, 1*, 2*, etc. (settings ignored) DockPanel (settings ignored) WrapPanel (ignored) Setting Width to RelativeSource of parent for ActualWidth (ignored)

Any ideas? It seems like a significant number of people have had this same problem, but I would highly prefer to not have to go the static width route for this column. Especially since the content just gets cut off when I do that anyway (even with height="Auto" for the row). The width of the overall window could be as small as 1024, but could also be 1600+ which is why I want dynamic sizing. That way smaller screens will have the content wrap and larger screens will just show the one line since the content fits.

Here is the XAML:

<ListView.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="45" />
                <ColumnDefinition Width="45" />
                <ColumnDefinition Width="45" />
                <ColumnDefinition Width="45" />
            </Grid.ColumnDefinitions>

            <!-- This is the TextBlock that needs to wrap its content (and
                 change the height of the row (so the full content is still
                 visible) to whatever the available space is, but should not
                 make overall ListView wider than the parent's width. -->
            <TextBlock Text="{Binding Content}" Padding="20,6,6,6" />                            

            <!-- These four blocks will have other content eventually, but only need
                 to be 45 wide -->
            <TextBlock Text="X" Grid.Column="1" HorizontalAlignment="Center" />
            <TextBlock Text="X" Grid.Column="2" HorizontalAlignment="Center" />
            <TextBlock Text="X" Grid.Column="3" HorizontalAlignment="Center" />
            <TextBlock Text="X" Grid.Column="4" HorizontalAlignment="Center" />
        </Grid>
     </DataTemplate>
</ListView.ItemTemplate>
Scott Salyer
  • 2,165
  • 7
  • 45
  • 82

3 Answers3

1

Not so easy...but it can be done.

I wrote a solution for you. In short, use Expression Blend to create a copy of the ListView Template and delete the ScrollViewer surrounding the ItemPresenter.

Here is a more indepth explanation:

How to have the TextBlock in a left column of a Grid in a ListView Template expand or shrink with text wrapping?

Rhyous
  • 6,510
  • 2
  • 44
  • 50
  • That is exactly what I was looking for! The only problem I have, and hopefully it is easy to resolve, is when the text wraps the size of the row the content is in doesn't change so the wrapped content disappears. I can see the top part of most of the characters, but not enough to read them. Thanks for getting me 99% there though! – Scott Salyer Jul 26 '11 at 18:27
  • On my box I don't see this with my test app. You must have something in your app setting the height explicitely or setting the VerticalAlignment to something other than stretch. Post your new XAML in the comments. – Rhyous Jul 26 '11 at 21:47
  • Ah - that was it. There was a ListView.ItemContainerStyle with a height value of 30. I took that off and this thing works EXACTLY how I wanted. If there was a way to give you more than one upvote I would. – Scott Salyer Jul 26 '11 at 23:13
0
<ListView HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListView.ItemTemplate>
        ...
    </ListView.ItemTemplate>
</ListView>
James
  • 9,774
  • 5
  • 34
  • 58
-1

I'd add TextWrapping="Wrap" to the first TextBlock element.

Mario Vernari
  • 6,649
  • 1
  • 32
  • 44
  • It had been added, but in my attempts of trying everything I must have accidentally removed it. The setting was ignored though. – Scott Salyer Jul 26 '11 at 17:59