Click here to Skip to main content
15,745,920 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I save / restore PropertyValue(Inline.TextDecorationsProperty) for a WPF Richtext Textrange?

My code saves / restores TextElement Properties (Bold, Italic or Font Color / Weight) but does not saves / restore values for Underline, strike through ...

Inline.TextDecorationsProperty should work for Underline - but does not work at all.

What else could I try?

What I have tried:

Public Class PropertyValues

        Public Property propVal1 As Object
        Public Property propVal2 As Object
        Public Property propVal3 As Object
        Public Property propVal4 As Object
        Public Property propVal5 As Object
        Public Property propVal6 As Object
        Public Property propVal7 As Object
        Public Property propInline As Object

Public Function SavePropertyValues(ByVal ActiveTBox As RichTextBox, ByVal selection As TextRange) As TextRange

            propInline = selection.GetPropertyValue(Inline.TextDecorationsProperty)
            propVal1 = selection.GetPropertyValue(TextElement.FontWeightProperty)
            propVal2 = selection.GetPropertyValue(TextElement.FontStyleProperty)
            propVal3 = selection.GetPropertyValue(TextElement.FontFamilyProperty)
            propVal4 = selection.GetPropertyValue(TextElement.FontSizeProperty)
            propVal5 = selection.GetPropertyValue(TextElement.FontStretchProperty)
            propVal6 = selection.GetPropertyValue(TextElement.ForegroundProperty)
            propVal7 = selection.GetPropertyValue(TextElement.BackgroundProperty)

            If propInline.Equals(TextDecorations.Underline) Then MessageBox.Show("prop 38 underline")
            If propInline.Equals(TextDecorations.Strikethrough) = True Then MessageBox.Show("strike through !")

            Return selection

End Function

Public Function RestorePropertyValues(ByVal ActiveTBox As RichTextBox, ByVal selection As TextRange) As TextRange

            If propInline.Equals(TextDecorations.Underline) = True Then
                MessageBox.Show("prop 52 underline !")
            End If

            selection.ApplyPropertyValue(Inline.TextDecorationsProperty, propInline)
            selection.ApplyPropertyValue(TextElement.FontWeightProperty, propVal1)
            selection.ApplyPropertyValue(TextElement.FontStyleProperty, propVal2)
            selection.ApplyPropertyValue(TextElement.FontFamilyProperty, propVal3)
            selection.ApplyPropertyValue(TextElement.FontSizeProperty, propVal4)
            selection.ApplyPropertyValue(TextElement.FontStretchProperty, propVal5)
            selection.ApplyPropertyValue(TextElement.ForegroundProperty, propVal6)
            selection.ApplyPropertyValue(TextElement.BackgroundProperty, propVal7)

            Return selection

End Function

End Class
Updated 7-Sep-22 7:25am
Comments 16-Sep-22 7:46am    
Who can help me solving this problem?

1 solution

"Underline", "Strike Through", etc. are values of the "Location" property.

How to: Create a Text Decoration - WPF .NET Framework | Microsoft Docs[^]
Share this answer
Comments 7-Sep-22 13:55pm    
Thanks for your answer but me is not able to get it right.

Your links are for a Textblock not for a textrange in a wpf richtextbox.

Can you please give me an example how the command for my code sample would be:

propInline = selection.GetPropertyValue( ?? )

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900