views:

481

answers:

2

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.

+1  A: 

Looks like the RichTextArea does not support this as yet in the Silverlight 4 beta, although it is possible to populate a RichTextArea with Paragraph elements with a particular TextAlignment in code and XAML you cannot do it on a Selection as this does not expose the "Block" or "Paragraph" element selected just the "Run".

RoguePlanetoid
This is no longer the case in the RC - SelectionAlignment is support in the RichTextArea.
RoguePlanetoid
A: 

Using silverlight 4 released version, this is how I did it. Editor.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Left)

That assumes that your text that you will be justifying is currently selected.

JOMan
Indeed this was fixed in the release candidate - thanks for posting it here anyway as I had forgotton about this question - I had a feeling at the time it was a beta only issue.
RoguePlanetoid