0

I know that when adding text/content/DataContext in XAML you refer to resource dictionary or inline mark up for styling around text or in template.

Q: However I'm having trouble trying to find a way to do the following:

Data is coming from a View Model/Model that is pulled from a database.

(string value) I am a <Bold>smart</Bold> man.

to show in a flow document like this:

I am a smart man.

Q end

Either by binding to a converter, behavior, or would saving the paragraph/document that I put in the flow document to a .rtf file in memory stream be a better option?

I've tried to utilize the option for behavior listed > here < but that is for text block and unable to redirect for type text instead of text block.

Trying to make it streamlined.

Tried to use data binding and apply the converter but even though I have the resource for the behavior / converter, it work due to the type conversion.

Community
  • 1
  • 1
Michael Edmison
  • 663
  • 6
  • 14

2 Answers2

0

One clever solution is presented by Rockford Lhotka in post Set rich text into RichTextBlock control. His idea is to create a custom control which then creates the RichTextBlock using XamlReader.Load.

This allows you to use code like the following:

    <local:RichTextDisplay Xaml="{Binding Hello}" HorizontalAlignment="Center" 
                           VerticalAlignment="Center"/>

Where Hello is:

    public string Hello { get; set; } = "I am a <Bold>smart</Bold> man.";

With a result:

Output xaml bold RichTextBlock

If you use UWP/Win 8.1 XAML, you can use the original code from the blog post with the following small change (Paragraphs added):

<UserControl 
  xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
  xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" 
  xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006""> 
  <Grid> 
    <RichTextBlock><Paragraph>");
            xaml.Append(ctl.Xaml);
            xaml.Append(@" 
    </Paragraph></RichTextBlock> 
  </Grid> 
</UserControl> 
");
Mikael Koskinen
  • 12,306
  • 5
  • 48
  • 63
0

To answer my own question: My case was creating a Document style display for user to update and save as a PDF, but I didn't want to rely on Office being on our application Server.

So I resolved this in my case by using a full "doc.RTF" document and importing that as a memory stream/string and apply my needed updates for values to that.

i.e. VB.net snippet example

Using uStream = Assembly.GetExecutingAssembly.GetManifestResourceStream("Resourcefilepath.rtf") 
    Using mStream As system.IO.MemoeryStream = New MemoryStream()
        uStream.CopyTo(mStream)
        rtfstring = Encoding.UTF8.GetSTring(mStream.toArray())
        '--Do the updates to the needed string as needed:
        rtfstring.Replace("Value","UpdatedValue")
        '--Load Property Memory String this method is returnind  
        RTFDataProperty = New MemoryStream(Encoding.UTF8.GetBytes(rtfstring))
    End Using
End Using

Then I loaded my XAML Rich Text Box with that memory stream as DataFormats.Rtf.

RichTextBox1.SelectAll()
RichTextBox1.Selection.Load(ClassName.RTFDataProperty, DataFormats.Rtf)

This gave me a template for formatting and layout of that document. (More of a case scenario and not a normal practice)

I also wanted to apply a starting selection so here is what I did there:

'--Get my RichTextBox Text
rtbtext As String = New TextRange(RichTextBox1.Document.contentStart, RichTextbox1.Document.ContentEnd).Text
Dim strStartSelection As String = "Comments..."
Dim startTP As TextPointer
Dim endTP As TextPointer

'--Loop through the paragraphs of the richtextbox for my needed selection starting point:
For Each para As Paragraph In RichTextBox1.Document.Blocks
    Dim paraText As String = New TextRange(para.ContentStart, para.ContentEnd).Text
    If paraText = "" Then
        Dim pos As TextPointer = para.ContentStart
        startTP = pos
        endTP = startTP.GetPositionAtOffset("".Length + 3) '--my string had ... on the end so had to add for avoiding the escape of that on length
        RichTextBox1.Selection.Select(startTP, endTP)
        RichTextBox1.Focus()
        Exit For
    End If
Next

This is the simple VB.net code layout, but you can simplify and adjust from there if you find it useful.

Thanks

Michael Edmison
  • 663
  • 6
  • 14