RichTextBox中的新行之后的空间 [英] Space After New Lines in RichTextBox

查看:96
本文介绍了RichTextBox中的新行之后的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户按下Enter或插入文本时,RichTextBox在行与行之间留出多余的空格,这就是我要避免的地方.我四处搜寻,发现的唯一不错的解决方案就是这个:

A RichTextBox puts extra space between lines when a user presses enter or inserts text, and that's what I'm trying to get away from. I searched around and the only decent solution I found is this one:

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);

但这仍然行不通.有人对我有提示吗?

But this still doesn't work. Does anyone have any tips for me?

推荐答案

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

I had the same problem, and i solved it by modifying the Xaml of the RichTextBox:

<RichTextBox>
    <RichTextBox.Resources>
        <Style TargetType="{x:Type Paragraph}">
            <Setter Property="Margin" Value="0"/>
        </Style>
    </RichTextBox.Resources>
</RichTextBox>

不知道这与您手动设置样式有何不同,但对我来说却奏效了.

Don't know how that's different than setting the style manually like you did, but for me it worked.

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

Update: To change it in code, you need to use the target type as key:

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

这篇关于RichTextBox中的新行之后的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆