2

我似乎无法弄清楚如何在新的 RichTextBox 中进行选择对齐,我有一个想法,我需要将选择转换为支持对齐的段落类型,但似乎无法弄清楚这一点。Silverlight 示例中没有一个具有此功能,但我确信这是可能的。
我有这段代码,它不起作用 - 因为我认为我需要选择一个段落,因为它总是返回并且异常“值不在预期范围内”。

Editor.Selection.SetPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Left)

我确保首先检查有效选择,如下代码适用于“粗体”:

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()

XAML 中的示例:

<Paragraph TextAlignment="Right">Example</Paragraph>

以上适用于 RichTextBox 的内容,但是我需要根据选择以编程方式执行此操作 - 就像在写字板中一样。

4

2 回答 2

2

使用silverlight 4 发布版本,我就是这样做的。 Editor.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Left)

这假设您将要证明的文本当前已被选中。

于 2010-07-02T22:18:52.370 回答
1

看起来 RichTextArea 在 Silverlight 4 beta 中尚不支持此功能,尽管可以在代码和 XAML 中使用具有特定 TextAlignment 的段落元素填充 RichTextArea,但您无法在选择上执行此操作,因为这不会公开“块”或“段落”元素只选择了“运行”。

于 2010-01-07T13:02:08.253 回答