How to do Selection Alignment using the RichTextBox in Silverlight 4?
- by RoguePlanetoid
I cannot seem to figure out how to do a Selection Alignment in the new RichTextBox, I have an idea that I need to convert the selection into the Paragraph type which supports alignment, but cannot seem to figure this out. None of the Silverlight examples have this, but I'm sure it is possible.
I have this code, that does not work - as I think I need the selection to be a Paragraph as it always returns and Exception "Value does not fall within the expected range".
Editor.Selection.SetPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Left)
I make sure I check for a Valid Selection first, code like the following works for "Bold":
If Editor.Selection.Text.Length > 0 Then ' Text Selected
If TypeOf Editor.Selection.GetPropertyValue(Run.FontWeightProperty) Is FontWeight _
AndAlso DirectCast(Editor.Selection.GetPropertyValue(Run.FontWeightProperty), FontWeight) = FontWeights.Normal Then
Editor.Selection.SetPropertyValue(Run.FontWeightProperty, FontWeights.Bold)
Else
Editor.Selection.SetPropertyValue(Run.FontWeightProperty, FontWeights.Normal)
End If
End If
Editor.Focus()
Example in XAML:
<Paragraph TextAlignment="Right">Example</Paragraph>
The above works in the contents of a RichTextBox however I need to do this programatically based on a selection - like in WordPad.