Show missing part of text in datagrid textbox WPF

北城以北 提交于 2019-12-13 00:36:57

问题


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.

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:


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>


来源:https://stackoverflow.com/questions/21982526/show-missing-part-of-text-in-datagrid-textbox-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!