如何将图像添加到我的Run for RichTextBlock?

Mar*_*len 1 c# wpf

我写了一个小的WPF应用程序,我喜欢将文本添加到RichTextBox中,以便最新的东西在顶部.我写了这个,它有效:

    /// <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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在我想制作一个新版本的功能,它也可以添加小图像.我虽然不知所措 - 是否可以添加图片?

rud*_*ler 9

请尝试以下方法:

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);
Run Code Online (Sandbox Code Playgroud)

InlineUIContainer在这里是"神奇的"......你可以添加任何UIElement.如果要添加多个项目,请使用面板来包装项目(即StackPanel等)