是否可以显示...
a 中某个单元格的内容DataGrid
是否大于指定的ColumnWidth
?就像是:
-------------------------------
| Name | Last Name |
-------------------------------
| LongNa.. | |
-------------------------------
TextTrimming
就是你要找的。使用DataGridTemplateColumn
并将 aTextBlock
插入CellTemplate
其中支持TextTrimming
。样本 -
<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>
是的你可以。
试试这个代码,我认为它会工作:
public static string StringEllipsis(DataGrid grid, string text, float columnWidth)
{
// you can customize this variables, for your preferences
string yourEllipsis = "...";
string yourFontFamily = "Arial";
float yourFontSize = 9F;
float yourTolerance = 1F;
Graphics graphics = grid.CreateGraphics();
if (graphics.MeasureString(text, new Font(yourFontFamily, yourFontSize)).Width <= (columnWidth + yourTolerance))
return text;
while (graphics.MeasureString(text + yourEllipsis, new Font(yourFontFamily, yourFontSize)).Width > (columnWidth + yourTolerance))
text = text.Substring(0, text.Length - 1);
return text + yourEllipsis;
}