1

Right now, I have the data I need binding to a RichTextBlock, but I'm unable to format it, since I'm just using one long string bound to the run on a paragraph setup in my RichTextBlock. Obviously, this isn't ideal. I would like to be able to change font sizes or font weights for some words, but can't see of a way to do that with my current method.

<RichTextBlock TextAlignment="Justify" TextWrapping="WrapWholeWords" Name="richTB">
    <Paragraph>
        <Run Text="{Binding}" />
    </Paragraph>
</RichTextBlock>

I know I can create a RichTextBlock in my code behind, and add paragraphs manually to that, but how would I bind it? Since it's in my page.resources?

m0ngr31
  • 791
  • 13
  • 29
  • What is the binding source? I mean, if it is a plain text, then how do you plan to get formatting info (this line is bold, this line is italic, etc.)? If it is a RTF document, then you'll need to perform manual conversion from RTF to the XAML's flow document (e.g., by attaching a binding converter to `RichTextBlock.Blocks` property). – Dennis Sep 25 '15 at 05:49
  • Currently, it's just one really long string. But I can change it to a bunch of `paragraph`'s pretty easily that can be formatted, I just don't know how I would bind that. – m0ngr31 Sep 25 '15 at 14:08

2 Answers2

0

Unfortunately you can't really bind it if you want to do custom formatting on the string. XAML builds those controls, and you can't really generate those controls through a binding the way you want.

What you could do is hook up an OnLoad event handler for your control, and inject whatever paragraph/run controls via C#.

Erik Elkins
  • 629
  • 5
  • 14
  • It would have to be bound. I can't inject the paragraphs in since it's a resource control. Being repeated up to 10 times on the page. – m0ngr31 Sep 25 '15 at 22:18
  • Yeah there aren't a lot of options. The best way might be to hook up the same loaded event for each RichTextBox. No code duplication that way. – Erik Elkins Sep 26 '15 at 00:54
0

I believe you are {binding} the source and hence inside the Code, you can take help from this code:

Paragraph para = new Paragraph();
            para.Inlines.Add(new Run { Text = sample, FontSize = 15 });            
            rtb.Blocks.Add(para);

where rtb is a richtextblock.

Kshitij Jhangra
  • 577
  • 7
  • 14