2010-04-16 57 views
1

我在加載內存流中的richtextbox時遇到了一些麻煩。從內存流中加載richtextbox。 WPF/VB> NET

我有一個數據庫表中的數據存儲爲一個字節數組,我將它轉換爲一個字符串並將其加載到一個內存流中,然後我想在richtextbox中加載該內存流。在

昏暗TR作爲新的TextRange(rtbTemplate.Document.ContentStart,rtbTemplate.Document.ContentEnd)申請減免

雖然。

代碼從數據庫

Dim TemplateData As Byte() = TemplateDataTableInstance.Rows(0).Item("TemplateData") 
    Dim strTemplateData As String 
    Dim enc As New System.Text.UTF8Encoding() 
    strTemplateData = enc.GetString(TemplateData) 

' I put a messagebox here to check if I get the data I want and I do 

獲取數據現在,我該如何理清休息嗎?我有

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 

和我的RichTextBox的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> 

任何幫助表示讚賞。

回答

2
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