以编程方式将图像添加到 RichTextBox 不会显示在 Xaml 属性中
尝试以编程方式从流将图像添加到 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
属性中。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不能(至少目前)。 RichTextBox 概述帮助文件显示:
It can't (at least currently). The RichTextBox Overview help file says:
正如 Otaku 所说,您不能将 XAML 属性用于图像。但是,您可以通过 RTB.Blocks 集合进行循环,并在块(段落)内通过 Inlines 集合进行循环。在那里您将找到带有图像对象的 InlineUIContainer。
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.