Friday, May 6, 2011

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?

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

    Arcturus : There are no stupid questions! :) Hope it solved your problem!
  • 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).

    Gustavo Cavalcanti : I agree Thomas. I started with type Image because for no good reason. :) It wasn't a good idea at all.

0 comments:

Post a Comment