开发者

How to do Selection Alignment using the RichTextBox in Silverlight 4?

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.FontWeightPro开发者_Go百科perty), 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.


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.


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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜