我需要你帮助我解决我遇到的问题。我有一个列表框,应该在从数据库中检索后显示Car Detials(右侧)和Car Image(左侧)。注意:将字节转换为数据库中的图像,工作正常。问题是图像不显示在列表框(左侧)中,而只显示汽车详细信息。缺少图像!我确保WPF样式中的Car和CarImage绑定。不知道我在代码或wpf风格中做错了什么。如果你愿意看看我的代码是什么问题,我会非常感激。非常感谢您的帮助。谢谢!
WPF:
<ListBox Style="{DynamicResource ListBoxStyle1}" DisplayMemberPath="Car" X:Name="listboxCars" />
WPF风格 - 汽车信息(右侧)和汽车图像(左侧):
<DataTemplate x:Key="templateListBoxItem">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Border Grid.Column="0"
Grid.Row="0"
Grid.RowSpan="2"
Margin="0,0,10,0">
<!-- binding it to Car Image from database -->
<Image Source="{Binding Path=CarImage}"
Stretch="Fill"
Height="40"
Width="40"></Image>
</Border>
<!-- same here to binding to Car -->
<TextBlock Text="{Binding Path=Car}"
FontWeight="Bold"
Grid.Column="1"
Grid.Row="0"></TextBlock>
</Grid>
</DataTemplate>
<Style x:Key="ListBoxStyle1" TargetType="{x:Type ListBox}">
<Setter Property="ItemTemplate" Value="{StaticResource ResourceKey=templateListBoxItem}"></Setter>
</Style>
此方法从数据库中检索具有自己图像的汽车信息列表
public List<CarInfo> GetCarImagesList (int days)
{
Cars = new List<CarInfo>();
const string sqlQuery = "select Car, CarImage from CarTemplates where Days = @days order by Car asc";
using (var command = new SqlCommand(sqlQuery, sqlConn))
{
try
{
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@days", days);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
byte[] buffer = new byte[10000];
var car = new CarInfo
{
Car = reader["Car"].ToString()
};
if (!reader.IsDBNull(reader.GetOrdinal("CarImage")))
{
long size = reader.GetBytes(reader.GetOrdinal("CarImage"), 0, buffer, 0, 10000);
using (MemoryStream strm = new MemoryStream())
{
strm.Write(buffer, 0, (int) size);
strm.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(strm);
car.CarImage = img;
}
}
Cars.Add(car);
}
}
.
.
.
return Cars;
}
最后一件事:当你点击按钮时,列表框将显示CarInformation(右侧)和CarImage(左侧)。在运行时我检查_databaseCarList中是否有CarInformations和CarImages的列表。图像(list.CarImage)甚至有值(字节),但为什么不显示图像?
private void Button1_Click(object sender, SelectionChangedEventArgs e)
{
_carImageList = new CarImageExtractor();
const int oneDay = 1;
var lstCar = new List<CarInfo>();
_databaseCarList = new ObservableCollection<CarInfo>(_carImageList.GetCarImagesList(oneDay));
if (_databaseCarList != null)
{
foreach (var list in _databaseCarList)
{
lstCar.Add(new CarInfo{Car = list.Car, CarImage = list.CarImage});
}
listboxCars.ItemsSource = lstCar;
}
}
答案 0 :(得分:0)
您正在使用WPF不支持的WindowsForms图像(System.Drawing.Image)作为Image的ImageSource。 (在VisualStudio中进行调试时,您应该从ImageSourceConverter获取绑定错误)
对于WPF,您需要使用BitmapImage。
将属性CarImage更改为System.Windows.Media.Imaging.BitmapImage
类型,并将图像的初始化代码更改为以下内容。
strm.Write(buffer, 0, (int) size);
strm.Position = 0;
BitmapImage img = new BitmapImage();
img.BeginInit();
img.StreamSource= strm;
img.EndInit();
car.CarImage = img;