gpt4 book ai didi

c# - 删除 WPF FlowDocument 左填充?

转载 作者:行者123 更新时间:2023-12-05 03:44:14 25 4
gpt4 key购买 nike

我正在尝试将流文档左对齐,不带填充,以便它与您在 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>

结果如下:

enter image description here

如您所见,红色是 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"

关于c# - 删除 WPF FlowDocument 左填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66586512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com