2012-03-27 91 views
7

當用戶按下Enter或插入文本時,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); 

但這仍然行不通。有沒有人對我有任何提示?

回答

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

抱歉..我沒有提及做到了這我需要在代碼中執行而不是在xaml中執行。但你的解決方案將工作得很好! – Jan 2012-03-28 07:27:38

+0

好的,發現問題了。更新了我的答案。 – Botz3000 2012-03-28 07:52:24

0

我財產厚度

my_paragraph.Margin = new System.Windows.Thickness(0); 
相關問題