如何在不打扰MarkUp的情况下从flowdocument段中删除字符串?

时间:2014-12-09 08:24:40

标签: c# wpf richtextbox flowdocument

成为RichTextBox和Googling的新手几个小时,我很难过。已经隔离了流程文档的段落部分。我现在希望删除所有文本,包括段落中的第一个“:” - 但保留段落的其余部分(包括其所有格式--XAML)。 (这在WPF vs2010中)。

我确定它很简单,但这是怎么做到的?

到目前为止,我将阅读第一个“:”,但您如何删除此文本?还有更好的方法吗?

  public InkRichViewModel(int p, Paragraph paragraph)
    {
        this.p = p;

        //Read the paragraph title from the paragraph
        TextPointer start = paragraph.ContentStart;
        TextPointer end = FindWordFromPosition(start, ":");

        TextRange textRange = new TextRange(start, end);
        String Title = textRange.Text;

        //Remove the title from the paragraph
        TextPointer endp = paragraph.ContentEnd;
        TextRange t2 = new TextRange(start, endp);

          ?????????????????????????????????????????


        this.Note = paragraph;   <---Note is the paragraph without the leading string.
    }

感谢所有帮助,问候。

1 个答案:

答案 0 :(得分:0)

就我而言,这解决了上述问题。还有更好的方法吗?

 // Read paragraph upto and including first ":"
        string text =  (paragraph.Inlines.FirstInline as Run).Text;
        int r = text.IndexOf(":")+1;
        string Title = text.Substring(0,r).Trim();

        // Remove Title from text;
        string newtext = text.Substring(r).Trim();

        // Insert new inline and remove the old inline.
        Inline z = paragraph.Inlines.FirstInline;
        Run runx = new Run(newtext);
        paragraph.Inlines.InsertBefore(z, runx);
        paragraph.Inlines.Remove(z);

        this.Note = paragraph;