tags:

views:

376

answers:

2

I wrote a small WPF app where I like to prepend text into a RichTextBox, so that the newest stuff is on top. I wrote this, and it works:

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

Now I would like to make a new version of that function which can add small images as well. I'm at a loss though - is it possible to add images?

A: 

RickTextbox.Document is a FlowDocument to which you can add almost anything that implements ContentElement. That includes Image, Label, StackPanel and all your other WPF favourites.

Check out the FlowDocument Overview for more details.

Jan Bannister
Thanks, but I've been playing around with it for a while and could not seem to get the right combination of objects to make it work.
Mark Allen
+3  A: 

Try the following:

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

The InlineUIContainer is the "magic" here... You can add any UIElement to it. If you want to add multiple items, use a panel to wrap the items (ie. StackPanel, etc)

rudigrobler
Aha - I could not get my head around the bitmap -> image -> inlineuicontainer -> paragraph thing. Thanks!!
Mark Allen