开发者

WPF flowdocument element names are reset?

开发者 https://www.devze.com 2022-12-14 20:52 出处:网络
I have a flowdocument with a named Span\" test1\"which I want to replace the contents of programmatically

I have a flowdocument with a named Span" test1" which I want to replace the contents of programmatically

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

run.Text = "replaced text";

This works, however the problem is when doing the update the name of the span is removed (looking at the xaml source). Is this by design? Is there any way to retain the Id's? Hi, this is the method I use for debugging whats actually in the richtextbox (setting xaml source to textbox)

using (MemoryStream ms = new MemoryStream()) { tr = new TextRange(this.richTextBox1.Document.ContentStart, this.richTextBox1.Document.ContentEnd); tr.Save(ms, DataFormats.Xaml); ms.Position = 0;

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

This is the test contents of the richtextbox:

<FlowDocument>
        <Paragraph>
            This is my first  <Span Name="test1">a huge amount of space</Span> between it and the second paragra开发者_如何学JAVAph?
        </Paragraph>
    </FlowDocument>


Unfortunately, this is just the way FlowDocuments work (same with tags). The only (very hacky) solution I have found for this is to hide a name in the FontFamily property. This works because this property is a comma delimited list of strings, with the first string that matches an existing font family being used, and the rest being ignored. So if you do something like this:

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

The whole string will be preserved. You may then access the "name" by searching the FontFamily for "name=".

Again, quite hacky, but after months of trying it was the only solution I could find.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号