Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Bind image property to Image control

Suppose I have string Name and Image Photo as properties of a class in my DataContext. I need to bind them to controls is a DataTemplate.

I thought this would work but it doesn't:

<Image Source="{Binding Photo}"/>

Why not? Should I my Photo have another type? (BitmapImage perhaps?)

How can I bind an Image control to an Image property?

Thanks!

Edit: As usual, after clicking submit I saw that the error is very clear: there's no converter from Image to ImageSource. How would my converter be to convert Image to ImageSource?

like image 737
Gus Cavalcanti Avatar asked Nov 06 '22 21:11

Gus Cavalcanti


2 Answers

It doesn't really makes sense to have a data property of type Image, since Image is a control (if you really want to do that you could bind to Photo.Source). You should use an ImageSource instead (or a BitmapImage, like you did, since BitmapImage inherits from ImageSource).

like image 68
Thomas Levesque Avatar answered Nov 15 '22 06:11

Thomas Levesque


Yes, if I make my property BitmapImage it all works just fine. Sorry for the stupid question.

like image 42
Gus Cavalcanti Avatar answered Nov 15 '22 07:11

Gus Cavalcanti