Saturday, August 27, 2011

Dynamic Image Source Changing in Silverlight OOB

Initialize Class:
xmlns:mycontrol="clr-namespace:yournamespacehere"

Implementing Static resources:

<UserControl.Resources >
<mycontrol:ImageUriConverter x:Key="converter"/ >
</UserControl.Resources >



Binding Image source: (it will automatically calling convertor class)

<Image Source="{Binding Path=ImagePath, Converter={StaticResource converter}}" Margin="5,5,6,5" Stretch="Fill"/ >


get Class for ImageUriConverter
http://code.google.com/p/photobucket-silverlight/source/browse/trunk/photobucketapi/ImageUriConverter.cs?r=46


Within Convert function you prepare dynamic path

string host = App.Current.Host.Source.ToString();
string imagePath = "";
if (host.IndexOf("/ClientBin") != -1) {
imagePath = host.Substring(0, host.IndexOf("/ClientBin"));
return new BitmapImage(new Uri(imagePath + "/" + value, UriKind.RelativeOrAbsolute));
}

//else no image path

return new BitmapImage(new Uri("/applicationname;component/Images/noimage.jpg", UriKind.RelativeOrAbsolute));



checkout also Sample:

http://www.eggheadcafe.com/sample-code/SilverlightWPFandXAML/03d69c15-172b-4098-bb90-5119f9bdac24/silverlight-ivalueconverter-for-image-urls.aspx


No comments:

Post a Comment