【发布时间】:2017-05-25 11:43:36
【问题描述】:
我正在使用“flowdocumentreader”来显示文本,“flowdocumentreader”的xaml代码很简单:
<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>
我将 .rtf 文档加载到“flowdocumentreader”,如下所示:
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 selection = ....; // this is the textrange of the string
var a = selection.GetPropertyValue(TextElement.BackgroundProperty)
但是,变量“a”总是返回 null。 :(
提前感谢您的宝贵时间。
编辑: 我加载到“FlowDocumentReader”中的 .rtf 文档具有背景颜色。有些是绿色的,有些是黄色的。
【问题讨论】:
标签: c# wpf background-color flowdocument