How to Set inline Images Vertically Center in RichTextBox

非 Y 不嫁゛ 提交于 2019-12-01 00:18:00

You can use BaselineAlignment on a Run to center align the text. Here is an example:

<RichTextBox>
    <FlowDocument>
        <Paragraph>
            <Run Text="Some text" BaselineAlignment="Center"/>
            <Image Height="100" Width="100" Source="Images\Desert.jpg"/>
            <Run Text="Some more text" BaselineAlignment="Center"/>
        </Paragraph>
        <Paragraph/>
        <Paragraph>
            <Run Text="Paragraph 2" BaselineAlignment="Center"/>
            <Image Height="100" Width="100" Source="Images\Desert.jpg"/>
            <Run Text="More text" BaselineAlignment="Center"/>
        </Paragraph>
    </FlowDocument>
</RichTextBox>

EDIT:

To apply the formatting to the entire RichTextBox try calling this method after the RichTextBox is populated:

    public void CenterText()
    {
        var text = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
        text.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Center);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!