在WPF richtextbox选择中删除项目符号样式

时间:2013-09-29 16:17:24

标签: wpf vb.net richtextbox

我已成功应用项目符号列表格式化,但如何再次删除它? 如何检测选择是否包含List? 我是不是太复杂了?是否有直接的方式将选择转换为项目符号列表并返回?

 Private Sub bullet(o As Windows.Forms.ContextMenuStrip, e As Windows.Forms.ToolStripItemClickedEventArgs)
    Dim lst As New Windows.Documents.List()
    lst.MarkerStyle = bullets(e.ClickedItem.Text)
    If rtf.Selection.IsEmpty Then
        lst.ListItems.Add(New Windows.Documents.ListItem())
    Else
        Dim li As Windows.Documents.ListItem
        Dim lines() As String = rtf.Selection.Text.Split(vbCrLf)
        For Each s As String In lines
            li = New Windows.Documents.ListItem()
            li.Blocks.Add(New Windows.Documents.Paragraph(New Windows.Documents.Run(s.Trim())))
            lst.ListItems.Add(li)
        Next
        rtf.Selection.Text = ""
    End If
    Dim curCaret = rtf.CaretPosition
    Dim curBlock = rtf.Document.Blocks.Where(Function(x) x.ContentStart.CompareTo(curCaret) = -1 AndAlso x.ContentEnd.CompareTo(curCaret) = 1).FirstOrDefault()
    rtf.Document.Blocks.InsertAfter(curBlock, lst)
    Dim vMove As Windows.Documents.TextPointer = Nothing

    vMove = curCaret.GetNextInsertionPosition(Windows.Documents.LogicalDirection.Forward)
    If vMove IsNot Nothing Then rtf.CaretPosition = vMove

    rtf.Focus()
End Sub

我已经从我的项目中删除了这段代码,因为它在某些情况下是不可靠的。基于动态XAML插入的解决方案是否更可靠? WPF的许多方面似乎都很难构思......

2 个答案:

答案 0 :(得分:0)

如果您可以使用richtextbox的Windows窗体版本,则可以使用SelectionBullet属性。

http://msdn.microsoft.com/en-us/library/ms742875.aspx

答案 1 :(得分:0)

尝试EditingCommands.ToggleBullets.Execute(null, richTextBox)在所选段落中切换项目符号样式。