2013-07-09 3 views
3

WPF에서 작업하고 있는데 RichTextBox에 RichText 데이터를 표시하고 있기 때문에 WinForm RichTextBox에서 Images + Text가있는 RichTextData를 표시하기 위해 WindowsFormHost를 가져 왔습니다.인라인 이미지를 세로로 가운데로 설정하는 방법

는 그러나 RichTextData 이미지를 상단과 텍스트에 정렬되어 디스플레이 하단에 정렬하는 동안, 아래 이미지를 참조하십시오, 빨간색 원은 내가 중앙에 이미지와 텍스트를 표시 할

enter image description here

RichTextImage입니다. 이미지 아래와 마찬가지로, Red Circle은 텍스트가 가운데에 오게되는 RichTextImage입니다.

<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

센터에서 텍스트 + 이미지를 정렬 할 수 없습니다입니다 :

enter image description here

내 XAML 코드입니다 RunBaselineAlignment을 사용하면 텍스트를 가운데 맞춤 할 수 있습니다. 다음은 예입니다

<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

고맙습니다. 지금 텍스트가 센터에 들어오고 있습니다.하지만 텍스트 서식이 사라졌습니다. –