2013-07-22 61 views
0

我將列表插入RichTextBox中,像這樣 - 但是如何將插入符移動到第一個列表項?WPF在插入列表後移動插入位置

感謝

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 

對不起它不會發布,如果我只是提出一個簡單的問題(它看起來像你的文章大多是代碼 - 請添加一些更多的細節),這樣xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

+0

嘿SO版主,你看看你的新的限制規則,在這裏做什麼?同樣的答案限制...請刪除它們! – Sheridan

回答

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 
+0

他們肯定是靈活的,FlowDocuments,但直觀,他們不是 – aelgoa

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; 

RichTextBox.CaretPosition Property MSDN上。

+0

這是我遇到的一個棘手問題:-) - 有人可以在文檔的任何位置插入一個列表,並且我希望插入位置從插入點移動到第一個列表項(在子彈後面) – gchq

+0

它如果您只需從當前插入位置導航到第一個列表項目,則可能不會太糟糕。這聽起來像它永遠也是一樣的運動。查看MSDN上的[TextPointer類](http://msdn.microsoft.com/en-us/library/system.windows.documents.textpointer.aspx)以獲取幫助。 – Sheridan