Gus
Gus

Reputation: 10659

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?

Upvotes: 5

Views: 11922

Answers (2)

Thomas Levesque
Thomas Levesque

Reputation: 292425

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).

Upvotes: 1

Gus
Gus

Reputation: 10659

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

Upvotes: 1

Related Questions