Search code examples
silverlightsilverlight-4.0c#-4.0silverlight-3.0

Setting a source property of Image in Silverlight using converter


I have a page where I want to display images in a control Template. For this I only get the Image name to show. So I am using a converter to return BitmapImage like this:

return new BitmapImage(new Uri(value.ToString(), UriKind.RelativeOrAbsolute));   

in the XAML binding i have:

Image Source="{Binding ThumbNail,Converter={StaticResource MapImagePath}}"/>

For some reason i am not able to see the image at all.

I have changed my image extension to .jpeg but still not working.

Am i doing some thing wrong or suggest me if I am wrong. Thanks.


Solution

  • Modify your converter so that it returns an instance of BitmapImage. Your converter is currently returning a string or a Uri however the actual type for the Source property is ImageSource.

    The conversion of a string to a BitmapImage is some magic that the XamlParser does for us but when we supply the value using a Converter we need to give it the correct type.