3

I have a RichTextBox containing two Runs:

<RichTextBox>
    <RichTextBox.Document>
        <FlowDocument>
            <FlowDocument.Blocks>
                <Paragraph Name="par">
                    <Run Text="First"/>
                    <Run Text="Second"/>
                </Paragraph>
            </FlowDocument.Blocks>
        </FlowDocument>
    </RichTextBox.Document>
</RichTextBox>

When I iterate through the Inlines of the par, I see three Runs: "First", " "(space) and "Second". It's ok. But if I delete second " " Run (using Backspace key, for example) and then iterate through the Inlines of the par, I see only one "FirstSecond" Run. RichTextBox merges two remaining Runs into single Run. However, if "First" and "Second" Runs have different TextFont or TextWeight values, RichTextBox won't merge them and I'll see two distinct Runs: "First" and "Second".

Now my question: How can I preserve adjacent Runs from being merged automatically by RichTextBox?

I would like to get the behaviour similar to the one when Runs have different TextFont or TextWeight values, but visually their formats should be equal. I've tried to set different Tag values for the different Runs, but it didn't help. Maybe, there is some "logical" format, that doesn't influence the appearance of the Runs, but warns RichTextBox to distinguish them.

4

1 回答 1

3

虽然我还没有找到明确的解决方案,但有一个解决方法。Run拥有Typography财产,而后者又拥有Int32 AnnotationAlternates财产。此属性定义了特定字体的特定字符的外观微调。如果此属性未按预期使用,它可以用作Run. 正如 MSDN 中所写:

如果 AnnotationAlternates 的值大于零并且所选字体不支持注释替代,则显示字母的默认形式。

在我的应用程序中,我使用Segoe UI字体,事实证明,它不支持注释替代,所以这个解决方法对我有用。如果你使用支持注解替换的字体,你可以尝试使用比较大的AnnotationAlternates属性值,也许不会影响文本的外观。

于 2014-10-10T20:19:52.497 回答