none
trim text in ListView/GridView cells

    Question

  • How do I get text in ListView/GridView cells to use ellipsis instead of wrap?

    TIA,

    Derek

    Friday, June 18, 2010 5:19 PM

Answers

  • Thanks! I did it this way, which seems to work:

    <!-- Control Template (ListViewItem) --> 
     <ControlTemplate x:Key="ListViewItemTemplate" TargetType="{x:Type ListViewItem}">
     	<Border x:Name="OuterBorder" ...">
     		<Border x:Name="InnerBorder" ...>
     			<GridViewRowPresenter...>
    				<GridViewRowPresenter.Content>
    					<TextBlock Text="{TemplateBinding Content}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
    				</GridViewRowPresenter.Content>
    			</GridViewRowPresenter>
     		</Border>
     	</Border>
     </ControlTemplate>
    • Marked as answer by Linda Liu Monday, June 28, 2010 7:22 AM
    Friday, June 18, 2010 8:53 PM

All replies

  • First of all you question actually made me think about implementing this in my program so thanks :)

    So i beleive those sites will help you implement this functionnality:

    An example about how to do this in a combox box:

    http://joshsmithonwpf.wordpress.com/2008/10/11/showing-an-ellipsis-for-clipped-text-in-a-combobox/

    And how to do it in a Datagrid:

    http://wpf.codeplex.com/Thread/View.aspx?ThreadId=78798

    I've tried the 2nd one in my code which works like a charm. It should now be quite easy to do the same for a gridview.

     

     <DataGridTextColumn Header="Name"
                         Binding="{Binding Path=FullNameWithPrefix, Mode=OneWay}">
                <DataGridTextColumn.ElementStyle>
    
                  <Style TargetType="TextBlock">
    
                    <Setter Property="TextWrapping" Value="NoWrap" />
    
                    <Setter Property="TextTrimming" Value="CharacterEllipsis" />
    
                  </Style>
    
                </DataGridTextColumn.ElementStyle>
    
    
              </DataGridTextColumn>

    I hope it helps.

     

    Friday, June 18, 2010 6:39 PM
  • Thanks! I did it this way, which seems to work:

    <!-- Control Template (ListViewItem) --> 
     <ControlTemplate x:Key="ListViewItemTemplate" TargetType="{x:Type ListViewItem}">
     	<Border x:Name="OuterBorder" ...">
     		<Border x:Name="InnerBorder" ...>
     			<GridViewRowPresenter...>
    				<GridViewRowPresenter.Content>
    					<TextBlock Text="{TemplateBinding Content}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
    				</GridViewRowPresenter.Content>
    			</GridViewRowPresenter>
     		</Border>
     	</Border>
     </ControlTemplate>
    • Marked as answer by Linda Liu Monday, June 28, 2010 7:22 AM
    Friday, June 18, 2010 8:53 PM
  • I do have a question, though. How would you specify this style so it applies to all datagrid text columns as an implicit style?

    Friday, June 18, 2010 9:01 PM
  • To be honest im not really sure how to do this...

    I know how to apply a style to lets say all Button or all DataGridColumnHeader.

    I believe the way to do this would be to redefine the template of the DataGridTextColumn, but that would be a pain.

     

    If you find an easy way to do this, lets me know :)

    Friday, June 18, 2010 10:46 PM