Simple Image Scroller Slide Show using Silverlight ListBox control
Last week I was working on a image scroll-er slide show using silverlight listbox control and it worked great and I thought I’d share this.
Click here to view the live demo (images may take a while to load)
Click here to download the article
If you find the Image Scroller code library useful, please consider donating

Xaml C# Code
public class ImageScroller
{
public string Url { get; set; }
}
public partial class ImageGallery : UserControl
{
int index = 0;
void StartSlideShow()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(2500); //delayed for 2.5 seconds
timer.Tick += (sender, e) =>
{
lbScrollGallery.ScrollIntoView(lbScrollGallery.Items[index]); //scroll to the current item
lbScrollGallery.SelectedIndex = index; //highlight the selected item in the list box scroller
ImageScroller item = (ImageScroller)lbScrollGallery.Items[index]; // getting the current item
imgPreview.Source = new BitmapImage(new Uri(item.Url, UriKind.Relative));
if (index < lbScrollGallery.Items.Count - 1)
{
index++;
}
else
{
lbScrollGallery.ScrollIntoView(lbScrollGallery.Items[0]); //scroll to the first item
index = 0; //reset the index when it reaches to the last item
}
};
timer.Start();
}
public ImageGallery()
{
InitializeComponent();
LoadImages();
StartSlideShow();
}
void LoadImages()
{
lbScrollGallery.ItemsSource = new List<ImageScroller>
{
new ImageScroller
{
Url = "images/Chrysanthemum.jpg"
},
new ImageScroller
{
Url = "images/Desert.jpg"
},
new ImageScroller
{
Url = "images/Hydrangeas.jpg"
},
new ImageScroller
{
Url = "images/Jellyfish.jpg"
},
new ImageScroller
{
Url = "images/Koala.jpg"
},
new ImageScroller
{
Url = "images/Lighthouse.jpg"
},
new ImageScroller
{
Url = "images/Penguins.jpg"
},
new ImageScroller
{
Url = "images/Tulips.jpg"
}
};
imgPreview.Source = new BitmapImage(new Uri("images/Chrysanthemum.jpg", UriKind.Relative)); //load default image while page loads
}
void ScrollerSelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listbox = (ListBox)sender;
ImageScroller item = (ImageScroller)listbox.SelectedItem;
if(item != null && ! string.IsNullOrEmpty(item.Url))
{
imgPreview.Source = new BitmapImage(new Uri(item.Url, UriKind.Relative));
index = listbox.SelectedIndex;
lbScrollGallery.ScrollIntoView(lbScrollGallery.Items[index]);
}
}
}
Hope this help and If you have any comments, please feel free to write your feedback.
Click here to view the live demo (images may take a while to load)
You can download the entire article from here
If you find the Image Scroller code library useful, please consider donating

Thanks
Deepu
Advertisement
Categories: Silverlight
Silverlight, Slide Show Image Scroller



Deepu,
thank you so much for sharing this app! I have been having a hard time to find something simple that works well and that I can understand since I am new to Silverlight and C#.
.mc
Deepu,
looks good but all I get is a message “this project has no startup scene……”
and all I get is a blank page. Any clues?
Hi Tony,
Please refer http://social.expression.microsoft.com/Forums/en/blend/thread/ad705525-4c3e-406d-9059-4831e166b44e
Thanks
Deepu
Just want to say what a great blog you got here!
I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!
Thumbs up, and keep it going!
Cheers
Christian, iwspo.net
Thanks Christian, Appreciated your feedback…
Regards
Deepu
I came up with this error:
The name ‘lbScrollGallery’ does not exists in the current context in MainPage.xaml.cs file?
Any help would be appreciated.
lbScrollGallery does exist in the ImageGallery.xaml file…
If you view the App.xaml file I have set the default xaml file as ImageGallery.xaml
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new ImageGallery();
}
Please let me know if you have any issues
Thanks
Deepu
Hello Deepu,
I am using your application, I want to navigate to a url when clicked on any image on the scroller instead of viewing it bigger in Grid. When ever any image is clicked from scroller it should navigate to url.
how can i implement that functionality into your code.
Can you help.
Thanks in advance.
Namrata.
Hi Namrata,
I have emailed the modified source code
Let me know if you need any help..
Thanks
Deepu
Hello Deepu,
Thanks for reply, I am new to silver light. Your solution is really helpful.
Can I make it also run in the sideshow.
Along with the providing links when clicked on particular image.
Thanks,
Yes Certainly
Thanks
Deepu
This is very nice article, can we use array for the imagescroller
Hi Deepu,
I am not able to dowmload source code. can u please forward the whole code so that i can use.
thanks for advance….
Done…
Thanks
Deepu
Hi!
I´m new in SL + C# and my problem is, that the gallery starts, but the pictures will not show on the screen. I have add the images in project and use also the same path.
Thank you very much for your help!!
Are you able to run my sample application
Thanks
Deepu
I have the same problem. I’m using Visual Studio 2010. Thank you!!
SOLVED!
Hello deepu its great work, m new to SL.. i converted ur code as to work as slide with both side arrows… front and back… now i want that slide to happen like iphone scroll… ie… smooth sliding… and stops slowly… without sudden stop… slowly it needs to stop…
if possible help me.. thanks in advance!!
You may try some thing similar in silverlight.net site
well my name is sandehs ..
and i m a part of the company ..
i want to standy this silverlight tools 0r applications ,… above code , whr i have to put this .. pls help me regarding this issue… i wil be great thank full u guys …
sndesh@indiatimes.com
You can take those files and add in to your existing application..
Thanks
Deepu
very nice article..keep it!
Hi ,
Just started using SL. Trying your app and it seems to work for me, but I cant see the pic at the bottom just above the scrollbar. I see that something is highlighted, but the pic/image can not be seen. Otherthan that it all works fine. Can you help me out with this issue
Jeh,
The attached sample is a working one… I am not sure.. what issue you are facing.. can you send me your code ?
Thanks
Deepu
Error 3 Closing tag for element ” was not found. D:\dotnet\wcf\SilverSlideShow\SilverSlideShow\MainPage.xaml 33 5 SilverSlideShow
Error 5 The attachable property ‘ItemsPanel’ was not found in type ‘ListBox’. D:\dotnet\wcf\SilverSlideShow\SilverSlideShow\MainPage.xaml 19 14 SilverSlideShow
sir i want bind selected item in listbox (images item) so plz me replay ..
Nice Article. I have implemented it. Thanks for Sharing.