【问题标题】:(C# WPF) How to change textrange background color?(C# WPF)如何改变文本范围的背景颜色?
【发布时间】: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


    【解决方案1】:

    但是,变量“a”总是返回 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>
    

    TextRange 的 TextElement.BackgroundProperty 属性没有默认值,这就是您第一次使用上述示例代码从 GetPropertyValue 方法返回空引用的原因。

    【讨论】:

    • 感谢您的回复。我加载到“FlowDocumentReader”中的 .rtf 文档有背景颜色,有些是绿色的,有些是黄色的。但是当我遍历文本范围时,我仍然得到空值。
    • 那么您能否提供此 .rtf 的样本?
    • 是的,当然。 .rtf 文档仅包含一个句子,如我编辑的帖子中所示。它仅用于测试,并且只有 1KB 大小。顺便说一句,请忽略 .rtf 文件的内容,它“是”一个 rtf 文件。 :)
    • 我不确定你的意思。你想要这个吗? {\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Georgia;}{\f3\fcharset0 DengXian;}}{\colortbl\red0\green0 \blue0;\red255\green255\blue255;\red255\green0\blue0;\red0\green255\blue0;\red255\green255\blue0;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\ fs21\f0\cf0 \cf0\qj{\f3 {\ltrch This }{\cf2\highlight3\ltrch is a tex}{\highlight3\ltrch t about to b}{\cf4\highlight3\ltrch e conv}{\ highlight3\ltrch 转为 rtf.}\li0\ri0\sa0\sb0\fi0\qj\par} } }
    【解决方案2】:

    已经有一段时间了,但终于找到了原因,问题是有时 Background 属性被添加到包含文本 Run 的 Span 中,我们要求的是 Run 背景颜色,而不是它的父级(Span 或段),我的问题/答案中的更多信息:

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

    【讨论】:

      猜你喜欢
      • 2013-01-15
      • 2018-07-31
      • 1970-01-01
      • 2013-08-03
      • 2018-04-14
      • 2019-09-10
      • 2019-06-26
      • 2010-09-12
      • 1970-01-01
      相关资源
      最近更新 更多