Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF toolkit datagrid cell text wrapping

My WPF datagrid's columns are fixed width, which means long text in the rows are cut off. How can I have the text wrap?

like image 923
Marcel Avatar asked Oct 01 '09 11:10

Marcel


3 Answers

If you are using a DataGridTextColumn, you need to define the Style for the DataGridTextColumn.ElementStyle

<dg:DataGridTextColumn Header="SomeLongText" Binding="{Binding MyText}">
  <dg:DataGridTextColumn.ElementStyle>
    <Style TargetType="TextBlock">
      <Setter Property="TextWrapping" Value="Wrap"/>
    </Style>
  </dg:DataGridTextColumn.ElementStyle>
</dg:DataGridTextColumn>

Full explination can be found at the following http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtextcolumn(v=vs.95).aspx

like image 149
James Avatar answered Oct 22 '22 22:10

James


You can replace the cell with a Textblock with Textwrapping enabled. i.e.

<dg:DataGridTemplateColumn Header="Description" Width="*">
   <dg:DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
           <TextBlock Text="{Binding Description}" TextWrapping="WrapWithOverflow"/>                                    
       </DataTemplate>
   </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
like image 5
Asif Avatar answered Oct 22 '22 21:10

Asif


If your DataGridTextColumn is being created in the code behind you can set the style and setters this way:

        _dataGridTextColumn.MaxWidth = 550;
        _dataGridTextColumn.ElementStyle = new System.Windows.Style(typeof(TextBlock));
        _dataGridTextColumn.ElementStyle.Setters.Add(new Setter(TextBlock.TextWrappingProperty, TextWrapping.Wrap));

This will cause the text inside the _dataGridTextColumn to wrap as it would inside a TextBlock.

like image 1
bPuhnk Avatar answered Oct 22 '22 21:10

bPuhnk