2017-01-10 38 views
0

私は "flowdocumentreader" のXAMLコードは単純で、テキストを表示するために "flowdocumentreader" を使用しています:(C#WPF)テキストの背景色を変更するには?

<FlowDocumentReader x:Name="myDocumentReader" ViewingMode="Scroll" VerticalAlignment="Stretch" ContextMenuOpening="myDocumentReader_ContextMenuOpening" Margin="0,0,0,0" Grid.Row="1" PreviewMouseDown="myDocumentReader_PreviewMouseDown"> 
    <FlowDocument x:Name="flow" LineHeight="{Binding ElementName=slider2, Path=Value}" PagePadding="{Binding ElementName=slider, Path=Value}"> 
     <Paragraph x:Name="paraBodyText"/> 
    </FlowDocument> 
</FlowDocumentReader> 

をそして、私はこのような "flowdocumentreader" への.rtf文書を読み込む:

paraBodyText.Inlines.Clear(); 
string temp = File.ReadAllText(dlg.FileName, Encoding.UTF8); 
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(temp)); 
TextRange textRange = new TextRange(flow.ContentStart, flow.ContentEnd); 
textRange.Load(stream, DataFormats.Rtf); 
myDocumentReader.Document = flow; 

今、私の質問は、 "flowdocumentreader"の文字列の背景色を取得する方法ですか?

文字列を検索する方法はわかっていますが、このような文字列の背景色を確認する方法はわかりません。それを行う方法はありますか?私は、文字列のTextRangeのを取得し、これを実行しようとしました:

TextRange selection = ....; // this is the textrange of the string 
var a = selection.GetPropertyValue(TextElement.BackgroundProperty) 

しかし、変数は「」常にnullを返します。お時間を事前に:(

おかげ

EDIT:。。。 私は「FlowDocumentReader」にロードされたの.rtf文書がいくつかは緑色で背景色を持っており、いくつかは、黄色で表示され

enter image description here

答えて

1

しかし、変数 "" 常にnullを返します。:(

あなたが実際に背景色?:

TextRange selection = new TextRange(flow.ContentStart, flow.ContentEnd); 
var a = selection.GetPropertyValue(TextElement.BackgroundProperty); 
selection.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Yellow); 
a = selection.GetPropertyValue(TextElement.BackgroundProperty); 

<FlowDocumentReader x:Name="myDocumentReader" ViewingMode="Scroll" VerticalAlignment="Stretch"> 
    <FlowDocument x:Name="flow"> 
     <Paragraph x:Name="paraBodyText"> 
      some text... 
     </Paragraph> 
    </FlowDocument> 
</FlowDocumentReader> 

を設定した場合、あなたが戻ってからnull参照を取得する理由ですTextRangeののTextElement.BackgroundPropertyプロパティのデフォルト値はありませんどのような10

上記のサンプルコードを使用して初めてGetPropertyValueメソッドを呼び出します。

+0

感謝。 "FlowDocumentReader"にロードされた.rtfドキュメントには背景色があり、一部は緑色で、一部は黄色です。しかし、私はまだテキスト範囲を横切るときにヌル値を持っています。 –

+0

この.rtfのサンプルを提供してください。 – mm8

+0

はい、もちろんです。 .rtf文書には、私の編集した投稿に示されている文が1つしか含まれていません。これはテスト用で、サイズはわずか1KBです。ところで、.rtfドキュメントの内容は無視してください。これはrtfドキュメントです。 :) –

0

これはしばらくしていますが、最終的にはなぜ、バックグラウンドプロパティがRunというテキストを含むSpanに追加され、その親ではなくRunの背景色を要求しているという問題が発見されました。私の質問/回答で段落)、より多くの情報:返信用

C# WPF RichText Box BackgroundProperty returns null when reading from file

関連する問題