【问题标题】:How to do Selection Alignment using the RichTextBox in Silverlight 4?如何在 Silverlight 4 中使用 RichTextBox 进行选择对齐?
【发布时间】:2010-12-31 02:06:14
【问题描述】:

我似乎无法弄清楚如何在新的 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 的内容,但我需要根据选择以编程方式执行此操作 - 就像在写字板中一样。

【问题讨论】:

    标签: silverlight properties silverlight-4.0 richtextbox selection


    【解决方案1】:

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

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

    【讨论】:

    • 确实,这已在候选发布版中得到修复 - 感谢您在此处发布它,因为我忘记了这个问题 - 当时我有一种感觉,它只是一个测试版问题。
    【解决方案2】:

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

    【讨论】:

    • RC 中不再是这种情况 - RichTextArea 支持 SelectionAlignment。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 2011-09-08
    • 2011-03-19
    相关资源
    最近更新 更多