Akash Kava
Akash Kava

Reputation: 39916

What is code equivalent of XAML to preserve whitespace?

What is c# code equivalent of following XAML, where I have a RichTextBox and I have selected Paragraph, and I want to enable/disable white space on this paragraph. In XAML I know how to enable, but I need to do this in code.

<Paragraph xml:space=\"preserve\">     Tabbed Code</Paragraph>

There is an equivalent I have found and its here,

void EnableWhiteSpace(Paragraph p, bool enable = true){
   if(enable){
         System.Windows.Markup.XmlAttributeProperties
              .SetXmlSpace(this.Document, "preserve");
   }
   else{
         System.Windows.Markup.XmlAttributeProperties
              .SetXmlSpace(this.Document, "default");
   }
}

This is still not working !!! I am not getting tabs !!...

Here is my problem, I have a RichTextBox which is used to edit code and which does syntax highlighting. Everything is fine except when I call following I see no tabs in my code.

 TextRange tr = new TextRange(
                         myRichTextBox.Document.ContentStart,
                         myRichTextBox.Document.ContentEnd);
 string text = tr.Text;

The text that I receive contains no tabs, so I thought enabling whitespace on every paragraph before doing text range might give me tabs.

UPDATE

I tried navigating every inlines (run) in the paragraph, none contains tab, I am just loosing all the tabs :(

Upvotes: 2

Views: 1913

Answers (2)

Akash Kava
Akash Kava

Reputation: 39916

RichTextBox is buggy, it will just ignore all tabs, however its problem of WPF itself and cant be fixed without doing complex workarounds.

Alernative is to write your own FlowDocument to text converter, however this is very complex as nodes do not directly give any line or tag information.

Upvotes: 0

Vlad
Vlad

Reputation: 35594

There is no equivalent. The XML option xml:space="preserve" is valid only for interpreting XML files (in your case, XAML, which is a kind of XML), and has no meaning in C# as there are no XML files involved.

The C# equivalent of your XAML code would be following:

Paragraph p = new Paragraph();
p.Inlines.Add(new Run("     Tabbed Code"));

Upvotes: 3

Related Questions