0

我有一个名为 Span“test1”的流文档,我想以编程方式替换它的内容

TextRange tr = new TextRange(this.test1.ContentStart,this.test1.ContentEnd);
Run run = this.test1.Inlines.First() as Run;

run.Text = "replaced text";

这可行,但是问题是在进行更新时,跨度的名称被删除(查看 xaml 源代码)。这是设计使然吗?有没有办法保留身份证?嗨,这是我用来调试richtextbox中实际内容的方法(将xaml源设置为文本框)

使用 (MemoryStream ms = new MemoryStream()) { tr = new TextRange(this.richTextBox1.Document.ContentStart, this.richTextBox1.Document.ContentEnd); tr.Save(毫秒,DataFormats.Xaml);ms.位置 = 0;

            using (StreamReader sr = new StreamReader(ms))
            {
                this.textBox1.Text = sr.ReadToEnd();
            }
        }

这是richtextbox的测试内容:

<FlowDocument>
        <Paragraph>
            This is my first  <Span Name="test1">a huge amount of space</Span> between it and the second paragraph?
        </Paragraph>
    </FlowDocument>
4

1 回答 1

0

不幸的是,这正是 FlowDocuments 的工作方式(与标签相同)。我为此找到的唯一(非常hacky)解决方案是在 FontFamily 属性中隐藏一个名称。这是有效的,因为此属性是一个逗号分隔的字符串列表,其中第一个字符串与正在使用的现有字体系列匹配,其余的被忽略。因此,如果您执行以下操作:

document.FontFamily = "RealFont1, RealFont2, name=test1";

整个字符串将被保留。然后,您可以通过在 FontFamily 中搜索“name=”来访问“name”。

再次,非常hacky,但经过几个月的尝试,这是我能找到的唯一解决方案。

于 2013-02-24T19:43:22.480 回答