以编程方式将图像添加到RichTextBox不会在Xaml属性中显示

时间:2010-03-27 16:43:36

标签: silverlight silverlight-4.0 richtextbox

尝试从Stream中以progamatically方式将图像添加到RichTextBox。图像显示在文本框中,但是在读取Xaml属性时,图像没有标记。

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

在运行时将图像插入RichTextBox以便将其保存到数据存储的正确方法是什么?在Xaml属性中。

2 个答案:

答案 0 :(得分:2)

它不能(至少目前)。 RichTextBox Overview帮助文件说:

  

Xaml返回的XAML字符串   财产不包括任何   存在的UIElement个对象   内容。

答案 1 :(得分:2)

正如Otaku所说,你不能将XAML属性用于图像。但是,您可以循环使用RTB.Blocks集合,并在块(段落)循环内部通过Inlines集合。你会在那里找到带有图像对象的InlineUIContainer。