我试图在TextBlock
中显示DateTime
的2019-10-07 17:00
。文本应该加下划线和虚线。为此,我使用了以下xaml
<TextBlock Text="2019-10-07 17:00">
<TextBlock.TextDecorations>
<TextDecoration Location="Underline">
<TextDecoration.Pen>
<Pen Brush="Black">
<Pen.DashStyle>
<DashStyle Dashes="5"/>
</Pen.DashStyle>
</Pen>
</TextDecoration.Pen>
</TextDecoration>
</TextBlock.TextDecorations>
</TextBlock>
但是,这会产生一些非常意外的结果,似乎每个连字符都会导致虚线下划线重新开始呈现。请注意,在每个连字符之后,虚线图案看起来几乎是随机的。
如果我将“减号连字符”更改为“不间断连字符”,看起来非常相似(- vs ‐),则渲染效果如预期。
<TextBlock Text="2019‐10‐07 17:00" ...>
每当我在文本中添加减号-连字符时,虚线下划线的错误呈现就会发生,但在我能找到的任何其他字符中却不会发生。是否有人注意到了这一点?是否有人有解决方案?如果没有,这种奇怪行为的原因可能是什么?
4条答案
按热度按时间8yoxcaq71#
根据您的格式,大小应该始终大致相同,这样您就可以使用另一个文本块,并让它覆盖另一个框
q3aa05252#
这可能是在WPF字形呈现代码中发现的一些奇怪的破折号破解的结果。在.NET源代码中,您将找到AdjustAdvanceForDisplayLayout()方法及其注解:
在
TextBlock
上设置TextOptions.TextFormattingMode="Display"
将产生稍微不同的伪像:这告诉我们,我们确实找到了这个“变通办法”(参见
GlyphRun.cs
第1326行)。因此,问题是我们是否能以某种方式得到第三个变体,而不需要任何这些工件。到目前为止,我还没有成功,但我确实试图找到发生这种连字符检查的地方。它似乎发生在本机代码中。请参阅TextFormatterContext.cs和LoCreateContext。
ny6fqffe3#
我不知道为什么会出现这种奇怪的行为。看起来像是
Pen
创建的破折号Map到了TextDecoration
的修饰文本。这是有道理的,因为破折号或TextDecoration
通常会自动调整为例如字体大小。减号字符似乎会产生不同的间距。也许这种行为不会在使用等宽字体时发生。无论如何,您可以创建一个平铺的
DrawingBrush
,并将其赋给Pen.Brush
属性以创建虚线。您可以使用DrawingBrush.ViewPort
来更改虚线的位置或长度。Viewport
由四个值组成,实际上是一个Rect
,用于描述图块的位置和尺寸:x, y, width, height
。宽度和高度值越大,虚线越长。结果是一个由虚线和空格组成的均匀绘图:
这种方法的缺点是虚线的大小和位置不再适应字体的大小。
4jb9z9bj4#
最后,我们构建了一个名为
DashTextBlock
的自定义控件来解决这个问题。它从TextBox
派生而来,样式类似于TextBlock
加上TextDecoration
,TextDecoration
使用Pen
加上x1m5 n1 a,LinearGradientBrush
是根据“dash-properties”指定的内容和DashThickness
的粗细设置的。为了实现这一点,它使用
TextBox
方法GetRectFromCharacterIndex
来确定如何设置LinearGradientBrush
。TextBox.GetRectFromCharacterIndex Method
返回指定索引处字符边缘的矩形。
它会产生这样的结果
样品使用
** Jmeter 板文本块**
样式