Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show an ellipsis in a DataGrid if the content of a cell is greater than its width?

Tags:

c#

wpf

datagrid

Is it possible to show ... if the content of some cell in a DataGrid is greater than the specified ColumnWidth? Something like:

-------------------------------
|   Name   |    Last Name     |
-------------------------------
| LongNa.. |                  |
-------------------------------
like image 480
Oscar Mederos Avatar asked Oct 14 '12 01:10

Oscar Mederos


1 Answers

TextTrimming is what you are looking for. Use the DataGridTemplateColumn and insert a TextBlock into the CellTemplate which supports TextTrimming. Sample -

<DataGrid ItemsSource="{Binding ItemsSourceForYourGrid}">
   <DataGrid.Columns>
      <DataGridTemplateColumn Width="20">
         <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
              <TextBlock Text="{Binding Name}" TextTrimming="CharacterEllipsis"/>
            </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
like image 170
Rohit Vats Avatar answered Sep 22 '22 01:09

Rohit Vats