Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Display Image dynamically
3 replies. Latest Post by moemeka on October 16, 2008.
(0)
Rajeev BV
Member
17 points
38 Posts
10-16-2008 12:21 AM |
Is it possible to display images in a silverlight application say from an adpater class or an external dll? This class / dll would actually provide the images, which Silverlight would convert into bmp/ jpeg or something & then display
lingbing
Contributor
2249 points
406 Posts
10-16-2008 3:21 AM |
Hi, I think just Binding the image control's Source to the string of uri is ok, it is like:
<Grid x:Name="MyGrid"><Image Source="{Binding URL"}/></Grid>
and in you code behind, create a simple data class like:public class Data:INotifyPropertyChanged{ private string m_URL; public string URL { get{ return m_URL; } set { m_URL=value; OnPropertyChanged("URL"); } } #region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
#endregion}
then, if your code-behind, Data data=new Data();data.URL="http://xxxxx.com/xxx.png";//or "images/xxx.png";MyGrid.DataContext= data;
DataBinding will help you set the image source, and because the INotifyPropertyChanged interface, when you change data.URL property, the image's source will change too.
Hope it help! Regards.
10-16-2008 6:32 AM |
Thanks for the reply. But is it possible that the class itself returns an image & can that be bound in Silverlight application?
moemeka
147 points
69 Posts
10-16-2008 8:19 AM |
Hi,
yes there are lots of ways you can achieve this. silverlight can load images, files, zipz, silverlight libraries, and even other sl applications dynamically. On approach would be,
1) bind class to a page's datacontext (this.datacontext = <some_dynamic_image_class_instance>)
2) bind some image within the page to an appropriate property of the datacontext class
3) use whatever mechanism withing your dynamic image class to change the actual image returned by the property as needed.
every change will result in the picture changing on the page. from the standpoint of your page, it will not need to know where the image is comming from, just that this class provides it with images through it's 'image' property.
Hope this helps.