RichTextBox中新行之后的空格

当用户按下回车键或插入文本时,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解决了这个问题:

      

不知道这与手动设置样式有什么不同,但对我来说它有用。

更新 :要在代码中更改它,您需要使用目标类型作为键:

 Style noSpaceStyle = new Style(typeof(Paragraph)); noSpaceStyle.Setters.Add(new Setter(Paragraph.MarginProperty, new Thickness(0))); rtb.Resources.Add(typeof(Paragraph), noSpaceStyle); 

我做了它的属性厚度

  my_paragraph.Margin = new System.Windows.Thickness(0);