1

我需要你帮助我解决我遇到的问题。我有一个列表框,它应该在从数据库中检索到汽车详细信息(右侧)和汽车图像(左侧)后显示它们。注意:将字节转换为工作正常的数据库中的图像。问题是图像不显示在列表框(左侧)中,而只显示汽车详细信息。缺少图像!我确保以 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;

            }

    }
4

1 回答 1

0

您正在使用 WPF 不支持的 WindowsForms 图像 (System.Drawing.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;
于 2013-07-11T09:12:57.393 回答