WPF - 在RichTextBox中取消选中下划线或删除线

时间:2013-07-15 17:13:02

标签: wpf vb.net richtextbox

如果我为TextElement属性设置一个按钮,打开和关闭效果很好 - 无论是对于所选文本还是仅仅为了打字或关闭作为键入文本,按照此示例。

 Private Sub TextEditor_SwitchItalics(sender As Object, e As RoutedEventArgs)
    Try
        Dim vEditor As RichTextBox = TextEditorGrid.FindName("Controls_TextEditorRTF")
        With vEditor
            Select Case vEditor.Selection.GetPropertyValue(TextElement.FontStyleProperty)
                Case FontStyles.Normal
                    vEditor.Selection.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic)
                Case FontStyles.Italic
                    vEditor.Selection.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Normal)

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

使用TextDecorations我遇到了问题 - 我可以打开,并可以关闭所选文本但尝试取消选择,因为键入无效。关于如何解决这个问题的任何想法?感谢

Private Sub TextEditor_SwitchStrikethrough(sender As Object, e As RoutedEventArgs)
    Try
        Dim vEditor As RichTextBox = TextEditorGrid.FindName("Controls_TextEditorRTF")
        Dim SelectionRange As New TextRange(vEditor.Selection.Start, vEditor.Selection.End)
        If (SelectionRange.GetPropertyValue(Inline.TextDecorationsProperty).Equals(TextDecorations.Strikethrough)) Then
            For Each Item In TextDecorations.Strikethrough
                vEditor.Selection.ClearAllProperties()
            Next
        Else
            vEditor.Selection.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Strikethrough)
        End If
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

事实证明ClearAllProperties没有效果,但将TextDecorations设置为Nothing

Private Sub TextEditor_SwitchStrikethrough(sender As Object, e As RoutedEventArgs)
    Try
        Dim vEditor As RichTextBox = TextEditorGrid.FindName("Controls_TextEditorRTF")
        Dim SelectionRange As New TextRange(vEditor.CaretPosition, vEditor.CaretPosition)
        If (SelectionRange.GetPropertyValue(Inline.TextDecorationsProperty).Equals(TextDecorations.Strikethrough)) Then
            For Each Item In TextDecorations.Strikethrough
                vEditor.Selection.ApplyPropertyValue(Inline.TextDecorationsProperty, Nothing)
                'vEditor.Selection.ClearAllProperties()
            Next
        Else
            vEditor.Selection.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Strikethrough)
        End If
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub