2008-10-13 50 views
1

我写了一个小的WPF应用程序,我喜欢在RichTextBox中预先添加文本,这样最新的东西就在最上面。我写了这个,它的工作原理:如何将图像添加到RichTextBlock的Run中?

/// <summary> 
    /// Prepends the text to the rich textbox 
    /// </summary> 
    /// <param name="textoutput">The text representing the character information.</param> 
    private void PrependSimpleText(string textoutput) 
    { 
     Run run = new Run(textoutput); 
     Paragraph paragraph = new Paragraph(run); 

     if (this.RichTextBoxOutput.Document.Blocks.Count == 0) 
     { 
      this.RichTextBoxOutput.Document.Blocks.Add(paragraph); 
     } 
     else 
     { 
      this.RichTextBoxOutput.Document.Blocks.InsertBefore(this.RichTextBoxOutput.Document.Blocks.FirstBlock, paragraph); 
     } 
    } 

现在我想作出一个新的版本,该功能可以添加小图像以及。尽管我很茫然 - 是否可以添加图片?

回答

7

尝试以下操作:

BitmapImage bi = new BitmapImage(new Uri(@"C:\SimpleImage.jpg")); 
Image image = new Image(); 
image.Source = bi; 
InlineUIContainer container = new InlineUIContainer(image);    
Paragraph paragraph = new Paragraph(container); 
RichTextBoxOutput.Document.Blocks.Add(paragraph); 

的InlineUIContainer是“魔”这里......您可以任意的UIElement添加到它。如果你想添加多个项目,使用面板来包装项目(即StackPanel等)

+0

啊哈 - 我无法绕过位图 - >图像 - > inlineuicontainer - >段的事情。谢谢!! – 2008-10-15 20:16:59

1

RickTextbox.Document是一个FlowDocument,你可以添加几乎所有实现ContentElement的东西。这包括图像,标签,StackPanel和所有其他WPF收藏夹。

查看FlowDocument Overview了解更多详情。

+0

谢谢,但我一直在玩它一段时间,似乎无法得到正确的对象组合使其工作。 – 2008-10-15 20:17:33