【问题标题】:WPF - unselect underline or strikethrough in RichTextBoxWPF - 在 RichTextBox 中取消选择下划线或删除线
【发布时间】:2013-07-15 17:13:02
【问题描述】:

如果我为 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

【问题讨论】:

    标签: wpf vb.net richtextbox


    【解决方案1】:

    原来 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
    

    【讨论】:

      猜你喜欢
      • 2011-02-02
      • 2014-09-26
      • 1970-01-01
      • 2014-10-14
      • 2016-08-26
      • 2017-03-23
      • 1970-01-01
      • 2014-03-22
      • 2010-09-16
      相关资源
      最近更新 更多