我在 WPF 应用程序中使用称为Pigiarniq的自定义字体。我发现有时,文字会从使用这种字体的 TextBlocks 中消失。消失的单词会出现在它们所在行的末尾,或者可能出现在下一行,这让我认为文本换行有问题。我终于(经过很多困难)将问题隔离为一小段代码(注意:这只是确认问题发生的测试。这不是我程序的一部分):
<Window x:Class="Test_the_textbox_width.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="850">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid
HorizontalAlignment="Center">
<TextBlock HorizontalAlignment="Left"
Name="textBlock1"
VerticalAlignment="Top"
FontFamily="Pigiarniq%20Regular.ttf#Pigiarniq"
TextWrapping="Wrap"
FontSize="15"
Margin="10">
<TextBlock.Text>
You do not need to enter both manually. Rates are calculated using true depositional
(uncompacted) thicknesses, rather than measured present-day thicknesses.
</TextBlock.Text>
</TextBlock>
</Grid>
</Grid>
出现在“(未压缩)”之后的“厚度”一词不会出现。如果我使用窗口句柄手动调整窗口大小,它将重新出现。
注意:我已将 TextBlock 放在上面的 Grid 元素中,因为有时当 TextBlock 位于 Grid 中时会出现此问题。在另一个实例中,它位于 InlineUIContainer 内。还有其他情况。
我发现如果我明确设置宽度不会发生这种行为,例如如果我订阅 TextBlock 上的 Loaded 事件:
private void textBlock1_Loaded(object sender, RoutedEventArgs e)
{
textBlock1.Width = textBlock1.ActualWidth;
//textBlock1.Width = Double.NaN; //if you leave this in, you get the bad behaviour
}
很奇怪!关于如何防止这种情况发生的任何想法?我的老板真的很想使用这种字体,所以我不能只选择另一种字体(我从未见过任何其他字体都会发生这种情况)。我也不太热衷于观察布局中的每一个变化并适当地调整每个使用 Pigiarniq 的文本块的大小。