首页 > 解决方案 > 在 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>

标签: c#wpfcanvastextblock

解决方案


正如我在对该问题的评论中已经提到的,您可以将 的 设置TextTrimmingTextBlock指示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


推荐阅读