Show missing part of text in datagrid textbox WPF

2019-08-06 18:35发布

I have one data-grid in my project, and after getting strings into it one part of them can't fit, because they are too long. I want that my text-box columns have fixed size, so I don't wanna use "auto" width property for text-box, but I was wondering: Is there some kind of property that I can use for showing whole string optionally? Like for example: If string is to long show the part of it you can fit, and after that show three dots (...) or some symbol like that. After clicking on three dots show whole value of text-box. Or even showing a whole string after rolling over some text-box.

My data-grid looks like this.

enter image description here

There you can see that some too long string values are cut of.

This is the xaml code of text-boxes in data-grid.

<DataGrid Grid.Column="0" Grid.RowSpan="2" AutoGenerateColumns="False" Height="206" HorizontalAlignment="Left" Margin="12,265,0,0" Name="tabela" VerticalAlignment="Top" Width="556" SelectionChanged="tabela_SelectionChanged" ItemsSource="Binding MyObsCollection">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Type" Width="120" Binding="{Binding Type}"/>
        <DataGridTextColumn Header="MapTo" Width="120" Binding="{Binding MapTo}"/>
        <DataGridTextColumn Header="Name" Width="116" Binding="{Binding Name}"/>
    </DataGrid.Columns>     
</DataGrid>

1条回答
三岁会撩人
2楼-- · 2019-08-06 19:19

You can set TextTrimming to CharacterEllipsis on TextBlock to show ellipse in case text is larger than available size.

Also, you can show the complete text in Tooltip. This is how you do it for one DataGridTextColumn:

<DataGridTextColumn Width="20" Binding="{Binding Name}">
  <DataGridTextColumn.ElementStyle>
    <Style TargetType="TextBlock">
      <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
      <Setter Property="ToolTip" Value="{Binding Text, 
                                 RelativeSource={RelativeSource Self}}"/>
    </Style>
  </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
查看更多
登录 后发表回答