我有一个名为 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>