WPF Paragraph獲取或修改文本內容


一、說明

Paragraph繼承自Block,Block繼承自TextElement,在TextElement中

        //
        // 摘要:
        //     獲取表示元素中內容末尾的 System.Windows.Documents.TextPointer。
        //
        // 返回結果:
        //     表示 System.Windows.Documents.TextElement 中內容末尾的 System.Windows.Documents.TextPointer。
        public TextPointer ContentEnd { get; }
        //
        // 摘要:
        //     獲取表示元素中內容開頭的 System.Windows.Documents.TextPointer。
        //
        // 返回結果:
        //     表示 System.Windows.Documents.TextElement 中內容開頭的 System.Windows.Documents.TextPointerContext。
        public TextPointer ContentStart { get; }

通過獲取ContentStart點和ContentEnd點之間的內容,獲取段落內容:TextRand.Text

    //
    // 摘要:
    //     表示兩個 System.Windows.Documents.TextPointer 位置之間的所選內容。
    public class TextRange : ITextRange
    {
        // 參數:
        //   position1:
        //     標記用於組成新 System.Windows.Documents.TextRange 的所選內容的一端的固定定位點位置。
        //
        //   position2:
        //     標記用於組成新 System.Windows.Documents.TextRange 的所選內容的另一端的可移動位置。
        public TextRange(TextPointer position1, TextPointer position2);

實例代碼:

//獲取段落部分的內容
Paragraph p = doc.FindName("one") as Paragraph;
TextRange range = new TextRange(p.ContentStart, p.ContentEnd);
string str = range.Text;
MessageBox.Show(str);
//修改段落內容
p.Inlines.Clear();
p.Inlines.Add(new Run("天涯共此時"));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM