删除 WPF FlowDocument 左填充?
Remove WPF FlowDocument left padding?
我正在尝试将流文档左对齐,不带填充,以便它与您在 TextBlock 中看到的完全匹配。我重新创建了一个简单的例子来说明我基本上想要实现的目标。这是我目前所拥有的:
<Grid>
<TextBlock Foreground="Red" Height="Auto" TextWrapping="Wrap"
Margin="0" Padding="0" FontSize="50" FontFamily="Arial"
Text="Some text."/>
<RichTextBox BorderThickness="0" Background="Transparent" BorderBrush="Transparent" IsInactiveSelectionHighlightEnabled="False" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled"
Height="Auto"
Margin="0" Padding="0" FontSize="50" FontFamily="Arial" >
<FlowDocument PagePadding="0" LineStackingStrategy="BlockLineHeight">
<Paragraph Margin="0" Padding="0" TextIndent="0">Some text.</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
结果如下:
如您所见,红色是 TextBlock 版本,黑色是 RichTextBox/FlowDocument 版本。 FlowDocument 文本向右偏移了大约 5 个像素。我试图删除我知道的所有填充,但我仍然无法摆脱该偏移量。感谢任何帮助。
注意:此问题与 WPF: How to make RichTextBox look like TextBlock?
重复
此偏移量与 RichTextBox
控件中的插入符实现有关。
查看 .Net 4.8 源代码,在 RichTextBox.cs 文件中:
// Allocates the initial render scope for this control.
internal override FrameworkElement CreateRenderScope()
{
FlowDocumentView renderScope = new FlowDocumentView();
renderScope.Document = this.Document;
// Set a margin so that the BiDi Or Italic caret has room to render at the edges of content.
// Otherwise, anti-aliasing or italic causes the caret to be partially clipped.
renderScope.Document.PagePadding = new Thickness(CaretElement.CaretPaddingWidth, 0, CaretElement.CaretPaddingWidth, 0);
// We want current style to ignore all properties from theme style for renderScope.
renderScope.OverridesDefaultStyle = true;
return renderScope;
}
以及 CaretElement.cs 文件中的 CaretElement.CaretPaddingWidth
定义:
// Caret padding width to ensure the visible caret for Bidi and Italic.
// Control(TextBox/RichTextBox) must have the enough padding to display
// BiDi and Italic caret indicator.
internal const double CaretPaddingWidth = 5.0;
因此,您可以检查的唯一选项是将 RichTextBox
边距设置为 Margin="-5,0,0,0"
。
我正在尝试将流文档左对齐,不带填充,以便它与您在 TextBlock 中看到的完全匹配。我重新创建了一个简单的例子来说明我基本上想要实现的目标。这是我目前所拥有的:
<Grid>
<TextBlock Foreground="Red" Height="Auto" TextWrapping="Wrap"
Margin="0" Padding="0" FontSize="50" FontFamily="Arial"
Text="Some text."/>
<RichTextBox BorderThickness="0" Background="Transparent" BorderBrush="Transparent" IsInactiveSelectionHighlightEnabled="False" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled"
Height="Auto"
Margin="0" Padding="0" FontSize="50" FontFamily="Arial" >
<FlowDocument PagePadding="0" LineStackingStrategy="BlockLineHeight">
<Paragraph Margin="0" Padding="0" TextIndent="0">Some text.</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
结果如下:
如您所见,红色是 TextBlock 版本,黑色是 RichTextBox/FlowDocument 版本。 FlowDocument 文本向右偏移了大约 5 个像素。我试图删除我知道的所有填充,但我仍然无法摆脱该偏移量。感谢任何帮助。
注意:此问题与 WPF: How to make RichTextBox look like TextBlock?
重复此偏移量与 RichTextBox
控件中的插入符实现有关。
查看 .Net 4.8 源代码,在 RichTextBox.cs 文件中:
// Allocates the initial render scope for this control.
internal override FrameworkElement CreateRenderScope()
{
FlowDocumentView renderScope = new FlowDocumentView();
renderScope.Document = this.Document;
// Set a margin so that the BiDi Or Italic caret has room to render at the edges of content.
// Otherwise, anti-aliasing or italic causes the caret to be partially clipped.
renderScope.Document.PagePadding = new Thickness(CaretElement.CaretPaddingWidth, 0, CaretElement.CaretPaddingWidth, 0);
// We want current style to ignore all properties from theme style for renderScope.
renderScope.OverridesDefaultStyle = true;
return renderScope;
}
以及 CaretElement.cs 文件中的 CaretElement.CaretPaddingWidth
定义:
// Caret padding width to ensure the visible caret for Bidi and Italic.
// Control(TextBox/RichTextBox) must have the enough padding to display
// BiDi and Italic caret indicator.
internal const double CaretPaddingWidth = 5.0;
因此,您可以检查的唯一选项是将 RichTextBox
边距设置为 Margin="-5,0,0,0"
。