방법: 프로그래밍 방식으로 요소를 텍스트에 삽입
다음 예제에서는 두 개의 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;
}
}
}
Imports System.Windows
Imports System.Windows.Media
Imports System.Windows.Controls
Imports System.Windows.Documents
Namespace SDKSample
Partial Public Class InsertInlineIntoTextExample
Inherits Page
Public Sub New()
' Create a paragraph with a short sentence
Dim myParagraph As New Paragraph(New Run("Neptune has 72 times Earth's volume..."))
' Create two TextPointers that will specify the text range the Span will cover
Dim myTextPointer1 As TextPointer = myParagraph.ContentStart.GetPositionAtOffset(10)
Dim myTextPointer2 As TextPointer = myParagraph.ContentEnd.GetPositionAtOffset(-5)
' Create a Span that covers the range between the two TextPointers.
Dim mySpan As New Span(myTextPointer1, myTextPointer2)
mySpan.Background = Brushes.Red
' Create a FlowDocument with the paragraph as its initial content.
Dim myFlowDocument As New FlowDocument(myParagraph)
Me.Content = myFlowDocument
End Sub
End Class
End Namespace
아래 그림은 이 예제가 어떻게 보이는지 보여 줍니다.
참고 항목
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET Desktop feedback