TextBlock text wrapping in GridViewColumn.CellTemplate not working
Asked Answered
Q

2

19

I am having problems getting the TextWrapping to work in this example. Can anyone see what I am duing wrong here?

    <ListView Name="listViewReportedException" ItemsSource="{Binding ExceptionDetails}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Time" DisplayMemberBinding="{Binding Thrown}" Width="150" />
                <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}" Width="385">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock TextWrapping="Wrap" Text="{Binding}" Width="385"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Recover action" DisplayMemberBinding="{Binding Action}" Width="90"/>
            </GridView>
        </ListView.View>
    </ListView>
Quarterage answered 19/6, 2012 at 12:52 Comment(1)
Did you try to set ScrollViewer.HorizontalScrollBarVisibility="Disabled" to your ListView?Stucker
E
37

You are setting both DisplayMemberBinding and CellTemplate. Remove DisplayMemberBinding when you use CellTemplate. Also remove the Width for the TextBlock and it'll work

<ListView ...>
    <ListView.View>
        <GridView>
            <!-- ... -->
            <GridViewColumn Header="Description" Width="385">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock TextWrapping="Wrap" Text="{Binding Description}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <!-- ... -->
        </GridView>
    </ListView.View>
</ListView>
Epigynous answered 19/6, 2012 at 13:5 Comment(0)
L
1

Setting a style for TextBlock also worked for me:

<ListView ItemsSource="{Binding xxx}">
  <ListView.Resources>
     <Style TargetType="TextBlock">
        <Setter Property="TextWrapping" Value="Wrap"/>
     </Style>
  </ListView.Resources>
  <ListView.View>
    <GridView.Columns>
      <GridViewColumn Header="Inspection Item" DisplayMemberBinding="{Binding InspectionItem}"/>
  </ListView.View>
<ListView>

enter image description here

Libyan answered 15/11, 2021 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.