How to Set inline Images Vertically Center in RichTextBox

后端 未结 1 815
青春惊慌失措
青春惊慌失措 2021-01-06 20:38

I am working on WPF, i am display RichText data in RichTextBox for that have taken WindowsFormHost, inside that i am taking WinForm RichTextBox to display RichTextData which

相关标签:
1条回答
  • 2021-01-06 20:55

    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);
        }
    
    0 讨论(0)
提交回复
热议问题