■ TextElement 객체에서 XAML을 구하는 방법을 보여준다.
▶ TextElement 클래스 : XAML 구하기 예제 (C#)
1 2 3 4 5 6 7 8 9 |
using System.Windows.Documents; FlowDocument flowDocument; ... string xaml = GetXAML(flowDocument.Blocks.FirstBlock); |
▶ TextElement 클래스 : XAML 구하기 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
using System.Text; using System.Windows.Documents; #region XAML 구하기 - GetXAML(textElement) /// <summary> /// XAML 구하기 /// </summary> /// <param name="textElement">텍스트 엘리먼트</param> /// <returns>XAML</returns> public string GetXAML(TextElement textElement) { StringBuilder stringBuilder = new StringBuilder(); TextPointer textPointer = textElement.ElementStart; while(textPointer.CompareTo(textElement.ElementEnd) < 0) { switch(textPointer.GetPointerContext(LogicalDirection.Forward)) { case TextPointerContext.ElementStart : stringBuilder.AppendFormat ( "<{0}>", textPointer.GetAdjacentElement(LogicalDirection.Forward).GetType().Name ); break; case TextPointerContext.ElementEnd : stringBuilder.AppendFormat ( "</{0}>", textPointer.GetAdjacentElement(LogicalDirection.Forward).GetType().Name ); break; case TextPointerContext.EmbeddedElement : stringBuilder.AppendFormat ( "<{0}/>", textPointer.GetAdjacentElement(LogicalDirection.Forward).GetType().Name ); break; case TextPointerContext.Text : stringBuilder.Append(textPointer.GetTextInRun(LogicalDirection.Forward)); break; } textPointer = textPointer.GetNextContextPosition(LogicalDirection.Forward); } return stringBuilder.ToString(); } #endregion |