views:

750

answers:

2

Trying to add an image to a RichTextBox progamatically from a Stream. The image displays in the text box, however when reading the Xaml property there is no markup for the image.

    private void richTextBox3_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            FileInfo[] files = (FileInfo[])e.Data.GetData(DataFormats.FileDrop);
            using (Stream s = files[0].OpenRead())
            {
                InlineUIContainer container = new InlineUIContainer();
                BitmapImage bmp = new BitmapImage();
                bmp.SetSource(s);
                Image img = new Image();
                img.SetValue(Image.SourceProperty, bmp);
                container.Child = img;

                richTextBox3.Selection.Insert(container);
            }
        }
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        // this doesn't have the markup from the inserted image
        System.Windows.MessageBox.Show(richTextBox3.Xaml);
    }

What is the correct way to insert an image into the RichTextBox at runtime so that it can be persisted to a data store? In the Xaml property.

+1  A: 

It can't (at least currently). The RichTextBox Overview help file says:

The XAML string returned by the Xaml property will not include any UIElement objects that are present in the content.

Otaku
It is possible to get an image in there so it's not a UIElement and will get returned?
rotary_engine
not that I know of. it appears that only an `InlineUIContainer` will allow for an image. it does seem silly to not allow a `UIElement` to be returned with the string XAML - not sure on why they decided on that
Otaku
So there is no way for the end user to insert an image, then save that to an external source such as a database? Can anyone suggest another control or way to do this?
rotary_engine
A: 

As Otaku says, you can't use the XAML property for images. However, you could loop trough the RTB.Blocks collection, and inside the block (paragraph) loop trough the Inlines collection. There you'll find the InlineUIContainer with the image object.

Nacho