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 🙁
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.