In my previous post : WPF-LINQ To SQL Sample, I had to resort to modifying .dbml file to get the images stored as varbinary into the WPF ListView control. I had to change the Type of image fields to System.Byte[], instead of using System.Data.Linq.Binary. A better way to do this is to use a custom converter. WPF allows you to specify custom converter as part of the Binding in .xaml, like this –
<Image Grid.Row="2"
Source="{Binding Path=ThumbNailPhoto ,
Converter={StaticResource imageConverter}}"></Image>
The custom converter is declared in the same .xaml file as a resource –
<Window.Resources> <local:ImageDataConverter x:Key="imageConverter" /> </Window.Resources>
ImageDataConverter is implemented in .xaml.cs file –
public class ImageDataConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
byte[] image = ((System.Data.Linq.Binary)value).ToArray();
return image;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
The updated sample code can be downloaded here : WPF-LINQ to SQL Sample
License : Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
