Load richtextbox from memorystream. WPF/VB>NET
I have some trouble with loading a richtextbox from a memorystream.
I have some data in a database table stored as a byte array, I convert it to a string and load it into a memorystream and then I want to load that memory stream in the richtextbox. The application breaks on
Dim tr As New TextRange(rtbTemplate.Document.ContentStart, rtbTemplate.Document.ContentEnd)
though.
Code for getting the data from the database
Dim TemplateData As Byte() = TemplateDataTableInstance.Rows(0).Item("TemplateData")
Dim strTemplateData As String
Dim enc As New System.Text.UTF8Encoding()
strTemplateData = enc.GetString(TemplateDa开发者_StackOverflowta)
' I put a messagebox here to check if I get the data I want and I do
Now, how do I sort out the rest? I have
Dim strDataFormat As String = DataFormats.Rtf
Using ms As New MemoryStream(strTemplateData)
Dim tr As New TextRange(rtbTemplate.Document.ContentStart, rtbTemplate.Document.ContentEnd)
tr.Load(ms, strDataFormat)
End Using
and my richtextbox in xaml
<RichTextBox x:Name="rtbLetter">
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
<FlowDocument FontSize="12" FontFamily="Times New Roman">
</FlowDocument>
</RichTextBox>
Any help is appreciated.
Dim fd0 As FlowDocument = New FlowDocument
Dim strDataFormat As String = DataFormats.Rtf
Dim ba() As Byte = Text.Encoding.ASCII.GetBytes(strDataFormat)
Dim ms As IO.MemoryStream = New IO.MemoryStream(ba)
Dim tr As TextRange = New TextRange(fd0.ContentStart, fd0.ContentEnd)
tr.Load(ms, System.Windows.DataFormats.Rtf)
ms.Close()
rtbLetter.document = fd0
精彩评论