插入列表后WPF移动插入位置

时间:2013-07-22 23:40:59

标签: wpf list richtextbox caret

我将列表插入到这样的RichTextBox中 - 但是如何让Caret移动到第一个列表项?

 Private Sub TextEditor_BulletListAdd(sender As Object, e As RoutedEventArgs)
    Try
      Dim vEditor As RichTextBox = TextEditorGrid.FindName("Controls_TextEditorRTF")
        Dim vList As New List()
        vList.MarkerStyle = TextMarkerStyle.Disc
        Dim vRun As New Run()
        Dim vItem As New ListItem(New Paragraph(vRun))
        vList.ListItems.Add(vItem)
        Dim curCaret = vEditor.CaretPosition
        Dim curBlock = vEditor.Document.Blocks.Where(Function(x) x.ContentStart.CompareTo(curCaret) = -1 AndAlso x.ContentEnd.CompareTo(curCaret) = 1).FirstOrDefault()
        vEditor.Document.Blocks.InsertAfter(curBlock, vList)

    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub

Private Sub TextEditor_NumberListAdd(sender As Object, e As RoutedEventArgs)
    Try
        Dim vEditor As RichTextBox = TextEditorGrid.FindName("Controls_TextEditorRTF")
        Dim vList As New List()
        vList.MarkerStyle = TextMarkerStyle.Decimal
        Dim vRun As New Run()
        Dim vItem As New ListItem(New Paragraph(vRun))
        vList.ListItems.Add(vItem)
        Dim curCaret = vEditor.CaretPosition
        Dim curBlock = vEditor.Document.Blocks.Where(Function(x) x.ContentStart.CompareTo(curCaret) = -1 AndAlso x.ContentEnd.CompareTo(curCaret) = 1).FirstOrDefault()
        vEditor.Document.Blocks.InsertAfter(curBlock, vList)
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub

2 个答案:

答案 0 :(得分:1)

简单的部分是设置插入符的位置......棘手的部分是找到你想要设置它的地方的指针(除非那只是文档的开头或结尾):

RichTextBox rtb = new RichTextBox(flowDoc);
// Get the current caret position.
TextPointer caretPos = rtb.CaretPosition;
// Set the TextPointer to the end of the current document.

caretPos = caretPos.DocumentEnd; // <<< You need to find the correct position here

// Specify the new caret position at the end of the current document.
rtb.CaretPosition = caretPos;

来自MSDN上的RichTextBox.CaretPosition Property

答案 1 :(得分:1)

事实证明,我认为答案要简单得多: - )

 Dim vMove As TextPointer = curCaret.GetNextInsertionPosition(LogicalDirection.Forward)
        If Not vMove Is Nothing Then
            vEditor.CaretPosition = vMove
        End If

完整

Private Sub TextEditor_BulletListAdd(sender As Object, e As RoutedEventArgs)
    Try
      Dim vEditor As RichTextBox = TextEditorGrid.FindName("Controls_TextEditorRTF")
        Dim vList As New List()
        vList.MarkerStyle = TextMarkerStyle.Disc
        Dim vRun As New Run()
        Dim vItem As New ListItem(New Paragraph(vRun))
        vList.ListItems.Add(vItem)
        Dim curCaret = vEditor.CaretPosition
        Dim curBlock = vEditor.Document.Blocks.Where(Function(x) x.ContentStart.CompareTo(curCaret) = -1 AndAlso x.ContentEnd.CompareTo(curCaret) = 1).FirstOrDefault()
        vEditor.Document.Blocks.InsertAfter(curBlock, vList)
        Dim vMove As TextPointer = curCaret.GetNextInsertionPosition(LogicalDirection.Forward)
        If Not vMove Is Nothing Then
            vEditor.CaretPosition = vMove
        End If
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub