RichTextBox中新行之后的空格

时间:2012-03-27 20:56:38

标签: c# wpf richtextbox line space

当用户按下回车键或插入文本时,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);

但这仍然无效。有没有人对我有任何提示?

2 个答案:

答案 0 :(得分:15)

我遇到了同样的问题,我通过修改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);

答案 1 :(得分:0)

我用属性厚度

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