WPF ListView TextBlock TextWrapping

纵饮孤独 提交于 2019-12-11 07:48:15

问题


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>

回答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?




回答2:


<ListView HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListView.ItemTemplate>
        ...
    </ListView.ItemTemplate>
</ListView>



回答3:


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



来源:https://stackoverflow.com/questions/6825693/wpf-listview-textblock-textwrapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!