locked
Displaying a short string in a DataGrid cell RRS feed

  • Question

  • i there:

    I have a DataGrid bound to a CollectionViewSource ina ViewModel, which in turn receives it's data from a Data Model (I'm using the MVVM approach)

    One of the fields of the table is a string that won't fit in the screen, that's why I want to display something
    like "This customer says that his..." where the actual data is much longer.

    I want this DataGrid to be editable also, only that when the user enters the column of the large string, the
    real string (the longer) must show, and after leaving the cell the new long string goes to the database or to
    the collection, and the displayed string must become "bla bla bla..." again.

    Any suggestion?

    Friday, April 20, 2012 2:16 PM

Answers

  • You have two different templates for a cell in a DataGrid. One is the CellTemplate the other is the CellEditingTemplate. You can place a TextBlock in the CellTemplate and set the TextTrimming="WordEllipsis"property, so it shortens the Text. In the CellEditingTemplate you set a regular TextBox for editing.

    <dg:DataGridTemplateColumn 
         Header="Name" 
         Width="100">
           <dg:DataGridTemplateColumn.CellTemplate>
             <DataTemplate>
               <TextBlock Text="{Binding SomeText}" 
                      TextTrimming="WordEllipsis"
                       VerticalAlignment="Center"/>
             </DataTemplate>
           </dg:DataGridTemplateColumn.CellTemplate>
           <dg:DataGridTemplateColumn.CellEditingTemplate>
             <DataTemplate>
               <TextBox Text="{Binding SomeText, Mode=TwoWay}" />
             </DataTemplate>
           </dg:DataGridTemplateColumn.CellEditingTemplate>
         </dg:DataGridTemplateColumn>
     

     

     

    Friday, April 20, 2012 2:32 PM