Depechie
Depechie

Reputation: 6142

Formatting text with RichtTextBlock

Small question, anyone have a small example on different data formatting tips when using a RichTextBlock control in Windows8 - XAML?

Currently I'm MVVM binding the Paragraph element of the RichTextBlock to a large text property on my MVVM.

But I was wondering, how can I indicate where to add Line break, setting titles, etc...? Anyone any good tips on doing this?

Upvotes: 3

Views: 1867

Answers (1)

Sandrino Di Mattia
Sandrino Di Mattia

Reputation: 24895

I never worked with the RichTextBlock before but there's an interesting topic on the MSDN forum: http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/d215abeb-7acd-41c1-81ba-c73a0ab68785 where an MSFT employee explains you should use the Blocks property.

Now, the Blocks property is of type BlockCollection, meaning it can hold any type of block. In your ViewModel you could fill the property with an HTML string (containing your text with headers, paragraphs, ...). Then you should consider creating an attached behavior that reads the HTML and creates the required blocks (like Windows.UI.Xaml.Documents.Paragraph) to finally fill up the Blocks property.

Take a look at the following guide to create attached behaviors: http://dotnet.dzone.com/articles/dragflickbehavior-windows

<RichTextBlock ...>
    <MyBehaviors:Interaction.Behaviors>
        <MyBehaviors:RichTextBehavior HtmlText="{Binding ...}"/>
    </MyBehaviors:Interaction.Behaviors>
</RichTextBlock>

Upvotes: 2

Related Questions