【问题标题】:Show Images in WPF ListBox在 WPF 列表框中显示图像
【发布时间】:2010-10-20 12:14:23
【问题描述】:

我正在尝试将存储在 MSSql Db 中的图像显示为 varbinary,但在执行我的代码时它会自行挂起

代码如下...

XAML

<Window x:Class="ImageList.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ImageList"
    Title="Window1" Height="331" Width="575">
    <Window.Resources>
        <local:BinaryImageConverter x:Key="imgConverter" />
    </Window.Resources>
    <StackPanel>
        <ListBox Name="lstImages" ItemsSource="{Binding}" >
            <ListBox.Resources>
                <DataTemplate x:Key="data">
                    <Image Source="{Binding Path=ImageData, Converter={StaticResource imgConverter}}"/>
                </DataTemplate>
            </ListBox.Resources>
        </ListBox>        
        <Button  Height="25" Name="button1" Click="button1_Click">Button</Button>
        <Button  Height="25" Name="button2" Click="button2_Click">Load Images</Button>
    </StackPanel>
</Window>

C#

{
       private void button2_Click(object sender, RoutedEventArgs e)
        {
            ImageDataContext db = new ImageDataContext();
            var data = db.ImageDetails.Select(p => p.ImageData).ToList();
            lstImages.ItemsSource = data;
        }

    }

    public class BinaryImageConverter : IValueConverter
    {
        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is byte[])
            {
                byte[] ByteArray = value as byte[];
                BitmapImage bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.StreamSource = new MemoryStream(ByteArray);
                bmp.EndInit();
                return bmp;
            }
            return null;
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }

【问题讨论】:

    标签: c# wpf data-binding .net-3.5


    【解决方案1】:

    我会尝试一下。

    ListBox 中的每个项目都绑定到一个名为“ImageData”的属性:

    <Image Source="{Binding Path=ImageData, Converter={StaticResource imgConverter}}"/>
    

    ...但是当您设置要绑定 ListBox 的列表时,您只是将 ImageData 属性选择到列表中:

    var data = db.ImageDetails.Select(p => p.ImageData).ToList();
    lstImages.ItemsSource = data;
    

    所以你最终会得到一个List&lt;byte[]&gt; 或其他东西,而不是具有 ImageData 属性的对象列表。

    如果我猜对了,你有两个选择:

    1.将绑定改为直接绑定图片:

    <Image Source="{Binding Converter={StaticResource imgConverter}}"/>
    

    2. 更改 linq 查询以创建具有 ImageData 属性的对象:

    var data = db.ImageDetails.Select(p => new { ImageData = p.ImageData.ToArray() }).ToList();
    lstImages.ItemsSource = data;
    

    编辑向 linq 查询添加了 ToArray() 调用以反映 Prashant 的发现。

    【讨论】:

    • 嗯。好的...您是否尝试过在您的 Convert 方法中添加一个断点并检查它是否被命中,以及“if”语句是否成功?
    • 嗨,马特,您的建议在稍作修改后就奏效了,非常感谢 :)
    猜你喜欢
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 2018-07-18
    相关资源
    最近更新 更多