当用户按下输入或插入文本时,RichTextBox 会在行之间放置额外的空间,这就是我想要摆脱的。我四处搜索,我找到的唯一合适的解决方案是这个:
Setter SetParagraphMargin = new Setter();
SetParagraphMargin.Property = Paragraph.MarginProperty;
SetParagraphMargin.Value = new Thickness(0);
Style style = new Style();
style.TargetType = typeof(Paragraph);
style.Setters.Add(SetParagraphMargin);
rtb.Resources.Add("Style", style);
但这仍然不起作用。有人对我有什么建议吗?
最佳答案
我遇到了同样的问题,我通过修改 RichTextBox 的 Xaml 解决了它:
<RichTextBox>
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
</RichTextBox>
不知道这与您手动设置样式有何不同,但对我来说却有效。
更新 :要在代码中更改它,您需要使用目标类型作为键:
Style noSpaceStyle = new Style(typeof(Paragraph));
noSpaceStyle.Setters.Add(new Setter(Paragraph.MarginProperty, new Thickness(0)));
rtb.Resources.Add(typeof(Paragraph), noSpaceStyle);