How get a WPF Datagrid with cells that wrap text instead of truncating it?

WpfDatagridWord Wrap

Wpf Problem Overview


What must be done to get a WPF DataGrid with cells that wrap text instead of truncating it?

Right now when a text is bigger and don't fit in a column the text is truncated and users can't see it value cos the DataGrid's IsReadOnly property is true. What I want is that the text in cells be wrapped and the cell height (NO CELL WIDTH) increased the amount needed to show all the text.

Wpf Solutions


Solution 1 - Wpf

Thanks for your help @H.B., this did the trick for me (alignment is optional):

<DataGrid.Columns>               
    <DataGridTextColumn Header="Wrapped & centered" Binding="{Binding field}">
        <DataGridTextColumn.ElementStyle>
             <Style>                            
                 <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                 <Setter Property="TextBlock.TextAlignment" Value="Center"/>
             </Style>
         </DataGridTextColumn.ElementStyle>
    </DataGridTextColumn>
</DataGrid.Columns>

Solution 2 - Wpf

I made something similar to D.Rosados solution. Mine is however reusable if you have more columns that needs wrapping.

<UserControl.Resources>
    <Style TargetType="{x:Type TextBlock}" x:Key="WrapText">
        <Setter Property="TextWrapping" Value="Wrap"/>
    </Style>
</UserControl.Resources>

<DataGrid.Columns>
    <DataGridTextColumn IsReadOnly="False" Header="Address" 
     Binding="{Binding Address}" ElementStyle="{StaticResource WrapText}" Width="150"/>
</DataGrid.Columns>

Solution 3 - Wpf

You could try to template the cells with a TextBlock which has text-wrapping enabled.

Solution 4 - Wpf

Here is another solution in addtional to others

<DataGridTemplateColumn Header="MyFieldName" Width="150" >
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding MyField}"  TextWrapping="Wrap">
        <TextBlock.ToolTip>
          <TextBlock Text="{Binding MyField}"  />
        </TextBlock.ToolTip>
      </TextBlock>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Solution 5 - Wpf

Another simple way of setting text wrap for Editing and Text DataGrid columns is to specity the Binding property and TextWrapping property as following:

<DataGridTemplateColumn x:Name="ColumnName" Header="Column Header Goes Here">
        <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                         <TextBox Text="{Binding Path=DataBoundProperty, Mode=TwoWay}" TextWrapping="Wrap"/>
                </DataTemplate>
        </DataGridTemplateColumn.CellEditingTemplate>
       	<DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                	<TextBlock Text="{Binding Path=DataBoundProperty, Mode=OneWay}" TextWrapping="Wrap"/>
         	</DataTemplate>
	</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1785721View Question on Stackoverflow
Solution 1 - WpfD.RosadoView Answer on Stackoverflow
Solution 2 - WpfRahbekView Answer on Stackoverflow
Solution 3 - WpfH.B.View Answer on Stackoverflow
Solution 4 - WpfNoWarView Answer on Stackoverflow
Solution 5 - Wpfsheraz yousafView Answer on Stackoverflow