Space After New Lines in RichTextBox

谁说我不能喝 提交于 2019-12-21 04:34:05

问题


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?


回答1:


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



回答2:


I did it with property thickness

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


来源:https://stackoverflow.com/questions/9897582/space-after-new-lines-in-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!