c# - 在 wpf 中以固定宽度在文本块中设置冗长的文本
问题描述
我在画布背景上有网格,可以在图片中看到。在第一行,我想标记列。为此,我使用了 TextBlock。但是当字符数增加时,我无法看到 TextBlock 的全部内容。例如,当内容为 9990 时,我可以看到它,但在下一个标签中,内容为 10020,其中包含更多字符。我只能看到 1002。矩形大小为 30,即绘制网格。TextBlock Width 为 27,TextBlock 的 Margin 为 3。在此处输入图像描述我不想更改字体大小。
<Canvas x:Name="back_canvas" Height="12000" Width="{Binding CanvasWidth , UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,0,10,0" >
<Canvas.Background>
<DrawingBrush TileMode="Tile" Viewport="0,0,30,30" ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,30,30"/>
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Brush="Gray" Thickness="1"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Canvas.Background>
<ItemsControl ItemsSource="{Binding TimeAxis}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Name="horizontalLabels" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Margin="0,0,3,0" Width="27" Background="Red" Height="Auto" >
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
解决方案
正如我在对该问题的评论中已经提到的,您可以将 的 设置TextTrimming
为TextBlock
指示CharacterEllipsis
某些部分已被截断并添加带有完整字符串的工具提示。
这看起来像这样:
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" ToolTip="{Binding}" TextTrimming="CharacterEllipsis" Margin="0,0,3,0" Width="27" Background="Red" Height="Auto"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
这应该将大量数字10020
从| 1002 |
类似| 100... |
的工具提示更改为类似的工具提示10020
推荐阅读
- python - 在 postgreSQL 中定义的模式上的 PonyORM 多对多关系
- python - 使用 QThread 正确显示线程 ID
- php - 未使用 iFrame 保存 Laravel 会话
- javascript - Material UI - 奇怪的 KeyboardDateTimePicker 交互
- git - 如何重新设置 git 超级项目更改子模块的哈希值?
- r - 使用 R 进行条件过滤
- html - 金立手机的图像不支持边框半径,当我将边框半径赋予图像时,图像会隐藏
- dc.js - 综合图表上的边距
- ios - 几个小时后 GLKViewController 挂起
- vue.js - 如何根据使用 vuetify 选择的菜单显示内容?