【发布时间】:2010-04-26 21:22:26
【问题描述】:
我的 WPF 项目中有 ListBox,它被设置为“MyObjectCollection”的数据源。 我设法让 ListBox 显示我的集合,并让每个项目显示来自对象的两个字符串属性。该对象还包含一个图像,我如何让图像显示在 ListBox 中?
我目前正在使用以下代码绑定到我的数据源
<UserControl.Resources>
<DataTemplate x:Key="CustomerTemplate">
<Border BorderThickness="2" BorderBrush="silver" CornerRadius="5" Padding="1"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid>
<Image Source="{Binding Artwork}" Tag="{Binding Artwork}" VerticalAlignment="Stretch" ></Image>
<TextBlock Text="{Binding Name}" Foreground="#515151"
FontSize="16" HorizontalAlignment="Stretch"
FontWeight="Bold" />
<TextBlock Text="{Binding Length}" Foreground="#515151" Margin="0,25,0,0"
FontSize="10" HorizontalAlignment="Stretch"
FontWeight="Bold" />
</Grid>
</Border>
</DataTemplate>
</UserControl.Resources>
谢谢, 本
【问题讨论】:
-
图像控件期望源是一个 URI,而不是实际图像...你能告诉我们“艺术品”是什么类型吗?
-
艺术品是 System.Drawing.Image 类型
-
System.Drawing.Image是 WindowsForms 而System.Windows.Media.ImageSource是应该在 WPF 中使用的类型。正如布伦特所指出的,最简单的方法是使用Uri。 -
唯一的问题是我正在从 SQL Server 数据库加载图像,并且真的不想将图像保存到磁盘以便在网格中显示它。