在TextBlock(WPF)中显示图像 [英] Display images in TextBlock (WPF)

查看:597
本文介绍了在TextBlock(WPF)中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的聊天应用程序。当前,消息被绑定到这样的自定义样式的列表框(简化的XAML):

I'm working on a simple chat application. Currently the messages are binded to a custom-styled listbox like this (simplified XAML):

<ListBox ItemsSource="{Binding MessageCollection}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Text}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

现在,我希望能够将图像(如图形笑脸)放入显示的消息文本中。有什么方法可以使用TextBlock(或任何其他标准组件)来实现此目的,还是需要为此使用一些特殊控件?

Now I would like to be able to put images (like graphical smileys) into the displayed message text. Is there any way to achieve this using TextBlock (or any other standart component) or do I need to use some special control for this?

请先感谢

推荐答案

如果您想让图像实际上位于文本内部 (如表情符号),那么您将不得不做一些工作。这听起来像是我实际上想要用户控件的几次情况之一,其关键点是扫描文本以查找表情符号值并即时构建数据模板。

If you want the Images actually inside the text (like an emoticon), then you are going to have to do some work. This sounds like one of the few times I would actually want a User Control, the point of which would be one that scans the Text looking for emoticon values and building a Data Template on the fly.

请记住,您在XAML中可以执行的所有操作都可以在代码中进行,因此我考虑的代码将遵循以下一般思路:

Remember that anything you can do in XAML you can do in code, so the code I'm thinking of would follow this general idea:


  1. 扫描文本以获取表情符号值,
    创建数据
    元素的值列表。

  2. 创建一个DockPanel。

  3. 列表中的Foreach元素,添加
    TextBlock或Image
    (基于值)。

  4. 将此内容设置为DockPanel。

  1. Scan text for emoticon values and create a list of values for data elements.
  2. Create a DockPanel.
  3. Foreach element in the List, add either a TextBlock or an Image (based on value).
  4. Set this.Content to the DockPanel.

我认为这样实际上就是您要寻找的东西,但是如果您只需要一个Image,则ValueConverter建议会起作用。

I think something like this is actually what you are looking for, but if you want just an Image, then the ValueConverter suggestion would work.

这篇关于在TextBlock(WPF)中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆