如何在RichTextBox中垂直居中设置内嵌图像

时间:2013-07-09 06:50:11

标签: wpf richtextbox windowsformhost

我正在使用WPF,我在RichTextBox中显示RichText数据,因为我已经使用了WinForm RichTextBox来显示具有图像+文本的RichTextData。

但是当显示RichTextData图像与Top对齐并且文本与Bottom对齐时, 请参见下图,红色圆圈是RichTextImage

enter image description here

我想在中心显示图像和文字。就像下面的图像一样,红色圆圈是RichTextImage,它带有文本。

enter image description here

我的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,但是我也无法在中心对齐文字+图像

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

2 个答案:

答案 0 :(得分:7)

您可以在BaselineAlignment上使用Run来居中对齐文字。这是一个例子:

<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后调用此方法:

    public void CenterText()
    {
        var text = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
        text.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Center);
    }

答案 1 :(得分:0)

我能够通过将 passport.use(new LinkedInStrategy({ clientID: LINKEDIN_CLIENT_ID, clientSecret: LINKEDIN_CLIENT_SECRET, callbackURL: "http://127.0.0.1:8000/user/auth/linkedin/callback", scope: ['r_emailaddress', 'r_basicprofile'], passReqToCallback: true }, function (req, accessToken, refreshToken, profile, done) { req.session.accessToken = accessToken; process.nextTick(function () { return done(null, profile); }); })); linkedinRouter.route('/auth/linkedin') .get(passport.authenticate('linkedin', { state: 'SOME STATE' }), function(req, res){ // The request will be redirected to LinkedIn for authentication, so this // function will not be called. }); linkedinRouter.route('/auth/linkedin/callback') .get( passport.authenticate('linkedin', { failureRedirect: '/' }), function (req, res) { return res.send('hello'); }); 属性设置为 SpanBaseAlignment 进行处理。

"Center"