How to implement Text Wrapping within a DataGridTextColumn
wpf, xaml
Solution
I had the same question earlier today and solved it by the following:
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="TextBlock.TextWrapping" Value="Wrap" />
</Style>
</DataGridTextColumn.ElementStyle>
Problem
First off I am using WPF and DataGrid because I want Column Headers. However I am not married to this solution if there is a way to do this and still have column headers. Now I have done some extensive searching and tried various solutions that I found via googling but none of these seem to work or are not directly targeted at the issue I am having and further do not seem to be translatable either. * I tried the following: ``` <Style TargetType="DataGridCell" x:Key="LeftColumnStyle"> <Setter Property="FrameworkElement.HorizontalAlignment" Value="Left"/> <Setter Property="FontSize" Value="14"/> <Setter Property="TextBlock.TextWrapping" Value="Wrap" /> </Style> <DataGridTextColumn Header="Comments" HeaderStyle="{StaticResource LeftHeaderStyle}" CellStyle="{StaticResource LeftColumnStyle}" Binding="{Binding Path=Comments, UpdateSourceTrigger=PropertyChanged}" Width="250" /> ``` * But the wrapping did not work -- so I tried the following: ``` <DataGridTextColumn Header="Comments" HeaderStyle="{StaticResource LeftHeaderStyle}" Binding="{Binding Path=Comments, UpdateSourceTrigger=PropertyChanged}" Width="250" > <DataGridTextColumn.ElementStyle> <Style TargetType="{x:Type TextBlock}"> <Setter Property="TextBlock.TextWrapping" Value="Wrap" /> <Setter Property="TextBlock.TextAlignment" Value="Left"/> <Setter Property="TextBlock.FontSize" Value="14"/> <Setter Property="TextBlock.Width" Value="250"/> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> ``` * The column wraps but does not auto-size in height so it simply seems to be truncating I tried putting a Textblock directly within the DataGridTextColumn and bind the data to the Textblock but the DataGridTextColumn gives an error saying that it does not support direct content. So in a nutshell I am trying to word wrap the columns that have text that will go beyond the preset cell width (I have more than one of these -- Description and Comments along with 5 other cells that do not wrap because they never exceed the preset cell width) however I cannot seem to get the height to adjust accordingly. I am okay with all the columns expanding in height for a row if either/both of the wrapping columns need more height much like that which would happen in an excel spreadsheet but I cannot even get the specific columns to expand when word wrapping. Thanks in advance.