RichTextBox中新行之后的空格

Jan*_*Jan 7 c# wpf space line 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);
Run Code Online (Sandbox Code Playgroud)

但这仍然行不通.有没有人对我有任何提示?

Bot*_*000 15

我遇到了同样的问题,我通过修改RichTextBox的Xaml解决了这个问题:

<RichTextBox>
    <RichTextBox.Resources>
        <Style TargetType="{x:Type Paragraph}">
            <Setter Property="Margin" Value="0"/>
        </Style>
    </RichTextBox.Resources>
</RichTextBox>
Run Code Online (Sandbox Code Playgroud)

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

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

Style noSpaceStyle = new Style(typeof(Paragraph));
noSpaceStyle.Setters.Add(new Setter(Paragraph.MarginProperty, new Thickness(0)));
rtb.Resources.Add(typeof(Paragraph), noSpaceStyle);
Run Code Online (Sandbox Code Playgroud)