방법: 프로그래밍 방식으로 요소를 텍스트에 삽입
업데이트: 2007년 11월
다음 예제에서는 두 개의 TextPointer 개체를 사용하여 Span 요소를 적용할 텍스트 내의 범위를 지정하는 방법을 보여 줍니다.
예제
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Documents;
namespace SDKSample
{
public partial class InsertInlineIntoTextExample : Page
{
public InsertInlineIntoTextExample()
{
// Create a paragraph with a short sentence
Paragraph myParagraph = new Paragraph(new Run("Neptune has 72 times Earth's volume..."));
// Create two TextPointers that will specify the text range the Span will cover
TextPointer myTextPointer1 = myParagraph.ContentStart.GetPositionAtOffset(10);
TextPointer myTextPointer2 = myParagraph.ContentEnd.GetPositionAtOffset(-5);
// Create a Span that covers the range between the two TextPointers.
Span mySpan = new Span(myTextPointer1, myTextPointer2);
mySpan.Background = Brushes.Red;
// Create a FlowDocument with the paragraph as its initial content.
FlowDocument myFlowDocument = new FlowDocument(myParagraph);
this.Content = myFlowDocument;
}
}
}
아래 그림에서는 이 예제를 보여 줍니다.