【问题标题】:setting a lengthy text in textblock with fixed width in wpf在 wpf 中以固定宽度在文本块中设置冗长的文本
【发布时间】:2019-02-18 12:39:45
【问题描述】:

我在画布背景上有网格,可以在图片中看到。在第一行,我想标记列。为此,我使用了 TextBlock。但是当字符数增加时,我无法看到 TextBlock 的全部内容。例如,当内容为 9990 时,我可以看到它,但在下一个标签中,内容为 10020,其中包含更多字符。我只能看到 1002。矩形大小为 30,即绘制网格。 TextBlock Width 为 27,TextBlock 的 Margin 为 3。enter image description here我不想更改字体大小。

 <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>

【问题讨论】:

  • @IDarkCoder 我认为他们希望数字适合一行而不改变容器的大小或文本的大小。
  • 您需要增加容器的大小或减小字体的大小。如果您尝试强制以该大小写入文本,它将重叠到下一个容器上,这会更糟。最后一个选择是尝试一种不同的字体,自然更小而不减小实际字体大小
  • @IDarkCoder 我同意,不改变某些方面使其适合是不可能的。如果您对画布有大小限制,我会选择先更改字体大小。然后创建一个悬停,如果需要,将展开框以查看数据。
  • @Darnold 当然你也可以将TextBlockTextTrimming 设置为CharacterEllipsis 来表示某些部分被截断并添加带有完整字符串的工具提示。
  • @Darnold 我将其作为答案包含在内,希望对 OP 有所帮助。

标签: c# wpf canvas textblock


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2016-10-17
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 2012-03-24
    相关资源
    最近更新 更多