2017-03-24 4 views
0

XAMLでtext/content/DataContextを追加するときは、テキストまたはテンプレートをスタイリングするためのリソースディクショナリまたはインラインマークアップを参照することがわかります。リッチテキストボックスのFlowDocumentの文字列からテキストへのテキスト書式の適用

Q: は、しかし、私は次の操作を行うための方法を見つけようとしてトラブルを抱えている:

データがデータベースから引き出されるビューモデル/モデルから来ています。

(文字列値) I am a <Bold>smart</Bold> man.

はこのような流れの文書に表示する:

私はスマート男です。

コンバータ、行動に結合することによりQエンド

のどちらか、または私はより良い選択肢でメモリストリームで.rtfファイルにフロードキュメントに入れ段落/文書を保存しますか?

私は、>here <の挙動に対してこのオプションを利用しようとしましたが、それはテキストブロック用であり、テキストブロックではなくタイプテキストのリダイレクトができません。

合理化しようとしています。

データバインディングを使用してコンバーターを適用しようとしましたが、ビヘイビアー/コンバーターのリソースを持っていても、タイプ変換のために機能します。

答えて

0

は自分の質問に答えるために:小さな変更(段落を追加しました)私たちのアプリケーションサーバーで。

私はこの場合、完全な "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 

次に、そのメモリストリームを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コードのレイアウトですが、それが有用であると分かったら、そこから単純化して調整することができます。 XamlReader.Loadが割り当てられていないストリーム、XamlReader、またはXmlReaderのを取るよう

おかげ

0

ポストSet rich text into RichTextBlock controlのRockford Lhotkaによって、巧妙な解決策が1つ提示されます。彼の考えはXamlReader.Loadを使用してRichTextBlockを作成するカスタムコントロールを作成することです。

これは、次のようなコードを使用することができます:結果で

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

こんにちはです
<local:RichTextDisplay Xaml="{Binding Hello}" HorizontalAlignment="Center" 
          VerticalAlignment="Center"/> 

をごUWPを使用している場合

Output xaml bold RichTextBlock

/Win 8.1 XAMLでは、ブログ記事の元のコードを以下の私の場合は、更新してPDFとして保存するユーザーのためのドキュメントスタイルのディスプレイを作成していた が、私はOfficeがあることに依存したくなかった。

<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

上記の提案は動作しません。 –

関連する問題