2013-07-09 22 views
3

我正在WPF上工作,我在RichTextBox中顯示RichTextBox中的數據,這些數據已採用WindowsFormHost,裏面我正在將WinForm RichTextBox顯示爲具有圖像+文本的RichTextData。如何在RichTextBox中垂直設置內聯圖像

不過,雖然顯示的是RichTextData圖像對準頂部和文字對齊底部, 請參閱下面的圖片,紅圈是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" /> 

回答

7

您可以在Run上使用BaselineAlignment來居中對齊文本。這裏有一個例子:

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

謝謝,我試過,但我沒有手動插入圖像和文本,我有我的數據庫中的文本和圖像的RTF數據從那裏我必須綁定此RichTextBox –

+0

我編輯了我的答案。 –

+0

非常感謝,文本現在進入中心,但文本格式已經不存在 –