2017-03-24 35 views
0

我知道,在XAML中添加text/content/DataContext時,您可以參考資源字典或內聯標記以獲取圍繞文本或模板的樣式。將文本格式從字符串應用到richtextbox中的FlowDocument的文本

問: 但是我有麻煩試圖找到一種方法,做到以下幾點:

數據是從從數據庫中抽取視圖模型/型號的到來。

(字符串值) I am a <Bold>smart</Bold> man.

這樣一個流程文檔中顯示:

我是一個聰明的

Q結束

無論是通過結合一個轉換器,行爲,或將保存款/文件,我把流文件到內存流的.rtf文件是一個更好的選擇嗎?

我試圖利用行爲選項>here <但這是文本塊,無法重定向爲文本類型的文本而不是文本塊。

試圖使其精簡。

試圖使用數據綁定和應用轉換器,但即使我有行爲/轉換器的資源,它的工作原因是類型轉換。

回答

0

要回答我的問題: 我的案件創建用戶文檔樣式更新顯示並保存爲PDF格式,但我並不想依靠辦公室是在我們的應用服務器。

所以我解決了這個在我的情況下,通過使用完整的「doc.RTF」文件並導入作爲內存流/字符串,並應用我所需的更新值。

即VB。淨片斷例如

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 

然後我裝我的XAML格式文本框與存儲流作爲DataFormats.Rtf。

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

這給了我一個格式和佈局該文件的模板。 (更多案例情景,而不是通常的做法)

我也想申請一個開始選擇所以這裏是我做的有:

'--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 

這是一個簡單的VB.net代碼佈局,但如果你覺得它有用,你可以從那裏簡化和調整。

由於

0

Rockford Lhotka在Set rich text into RichTextBlock control後提出了一個聰明的解決方案。他的想法是創建一個自定義控件,然後使用XamlReader.Load創建RichTextBlock。

這樣您就可以使用如下代碼:

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

其中Hello是:

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

有了結果:

Output xaml bold RichTextBlock

如果使用UWP/Win 8.1 XAML,您可以使用以下文章中的原始代碼小的變化(段加):

<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> 
"); 
+0

上述建議將不起作用作爲XamlReader.Load需要流,XamlReader,或XmlReader中未分配的。 –

相關問題