如何在RichTextBox中将行内图像设置为垂直居中 [英] How to Set inline Images Vertically Center in RichTextBox

查看:342
本文介绍了如何在RichTextBox中将行内图像设置为垂直居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在WPF上工作,我在RichTextBox中显示已采用WindowsFormHost的RichText数据,在其中我正在WinForm RichTextBox上显示具有图像+文本的RichTextData.

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 have Images + Text.

但是在显示RichTextData图像与顶部对齐并且文本与底部对齐时, 请参见下面的图片,红色圆圈为RichTextImage

But while display that RichTextData images are align to Top and text are align to Bottom, See in Image below, red circle is RichTextImage

我想在中间显示图像和文本.像下方图片一样,红色圆圈是RichTextImage,它以文本为中心.

i want to display images and Text in center. Like Below Image, the Red Circle is RichTextImage that is coming in center with text.

我的XAML代码是:

<Window x:Class="WPFRichTextBox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    Title="MainWindow" Height="600" Width="800" Background="LightBlue" xmlns:my="clr-namespace:WPFRichTextBox">

<Grid Loaded="Grid_Loaded">

    <WindowsFormsHost Margin="0,424,0,22">

        <wf:RichTextBox   Text="RichTextBox" x:Name="richTbTest1" BorderStyle="None" Enabled="True" ForeColor="Black" Width="550" Multiline="True" />


   </WindowsFormsHost>

  </Grid>
</Window>

我也使用了WPF RichTextBox,但是我也无法在文本中对齐文本和图像

I have used WPF RichTextBox also, but in that also i am not able to Align text+Images in Center

     <RichTextBox VerticalContentAlignment="Stretch" Height="158" HorizontalAlignment="Left" Margin="10,247,0,0" Name="richTextBox1" VerticalAlignment="Top" Width="754" />

推荐答案

您可以在Run上使用BaselineAlignment使文本居中对齐.这是一个示例:

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>

要将格式应用于整个RichTextBox,请尝试在RichTextBox填充后调用此方法:

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);
    }

这篇关于如何在RichTextBox中将行内图像设置为垂直居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆