I am trying to create a "Contact book" app, using C# WPF, MVVM, SQLITE & DAPPER.
Issue: when i try to bind the Contact picture with an image element in my WPF user control the image is not displayed.
The other Contact properties bound to wpf elements seem to work(Contact name, details etc).
The byte array for the contact Picture gets initialized(i have manually added some test contacts and images to the table with DB Browser for SQLITE), but the image element doesn't display it.
What am i doing wrong?
What I have tried:
Contact Model:
public class Contact
{
public int Id { get; set; }
public string FullName { get; set; }
public byte[] Picture { get; set; }
public string Information { get; set; }
public string Phone { get; set; }
public string WhatsApp { get; set; }
public string Skype { get; set; }
}
XAML:
<UserControl.Resources>
<Converter:ByteArrayToBitmapImageConverter x:Name="binaryConverter" x:Key="byteToImageConverter"/>
</UserControl.Resources>
<Grid Margin="5" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="4" Grid.RowSpan="4" Background="DarkGoldenrod">
<Image Source="{Binding SelectedContact.Picture, Converter={StaticResource byteToImageConverter}}" />
</Grid>
Converter:
public class ByteArrayToBitmapImageConverter : IValueConverter
{
public BitmapImage ConvertByteArrayToBitMapImage(byte[] imageByteArray)
{
BitmapImage img = new BitmapImage();
using (MemoryStream memStream = new MemoryStream(imageByteArray))
{
img.StreamSource = memStream;
}
return img;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
BitmapImage img = new BitmapImage();
if (value != null)
{
img = this.ConvertByteArrayToBitMapImage(value as byte[]);
}
return img;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}