views:

154

answers:

1

I've overridden the key-handling events and am inserting tabs when a user presses the tab key for a RichTextBox.

When I save the XAML from that RichTextBox and reload it, all of the tabs are now spaces.

Does anyone know what I can do to get the RichTextBox to display tabs?

A: 

I got around this issue by putting a placeholder in temporarily.

private const string TAB = "    ";
private const string TAB_PLACEHOLDER = "===TAB===";

I used the placeholder to temporarily replace all of the tab characters and then once they were in the RichTextBox I replaced all of the placeholders with tabs.

textBox1.Text = richTextBox1.Xaml;
string xaml = richTextBox1.Xaml;

xaml = xaml.Replace(TAB, TAB_PLACEHOLDER);

richTextBox2.Xaml = xaml;

foreach (Block block in richTextBox2.Blocks)
{
    foreach (Inline inline in ((Paragraph)block).Inlines)
    {
        ((Run) inline).Text = ((Run) inline).Text.Replace(TAB_PLACEHOLDER, TAB);
    }
}
Brendan Enrick