Home > Silverlight > Simple Image Scroller Slide Show using Silverlight ListBox control

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
  1. Marcelo
    May 5, 2010 at 5:23 am | #1

    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

  2. May 11, 2010 at 9:58 am | #2

    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?

    • May 11, 2010 at 12:25 pm | #3

      Hi Tony,

      Please refer http://social.expression.microsoft.com/Forums/en/blend/thread/ad705525-4c3e-406d-9059-4831e166b44e

      Thanks
      Deepu

  3. May 16, 2010 at 12:01 am | #4

    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

    • May 16, 2010 at 2:18 pm | #5

      Thanks Christian, Appreciated your feedback…

      Regards
      Deepu

  4. mohamed
    May 20, 2010 at 7:53 am | #6

    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.

    • May 20, 2010 at 8:01 am | #7

      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

  5. namrata buddhadev
    July 28, 2010 at 8:32 pm | #8

    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.

    • July 29, 2010 at 2:54 am | #9

      Hi Namrata,

      I have emailed the modified source code

      Let me know if you need any help..

      Thanks
      Deepu

  6. Namrata
    July 29, 2010 at 5:31 pm | #10

    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,

    • July 30, 2010 at 2:32 am | #11

      Yes Certainly

      Thanks
      Deepu

  7. Gabe
    October 7, 2010 at 12:13 pm | #12

    This is very nice article, can we use array for the imagescroller

  8. Srikant
    October 21, 2010 at 12:21 pm | #13

    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….

    • October 23, 2010 at 12:24 pm | #14

      Done…

      Thanks
      Deepu

  9. Bernhard
    November 23, 2010 at 9:38 am | #15

    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!!

    • November 23, 2010 at 2:38 pm | #16

      Are you able to run my sample application

      Thanks
      Deepu

    • m
      February 13, 2011 at 10:15 am | #17

      I have the same problem. I’m using Visual Studio 2010. Thank you!!

      • m
        February 13, 2011 at 11:22 am | #18

        SOLVED!

  10. kishor
    February 25, 2011 at 1:08 pm | #19

    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!!

    • March 1, 2011 at 1:46 pm | #20

      You may try some thing similar in silverlight.net site

  11. March 19, 2011 at 8:09 am | #21

    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

    • March 20, 2011 at 4:31 pm | #22

      You can take those files and add in to your existing application..

      Thanks
      Deepu

  12. anil
    May 20, 2011 at 12:00 pm | #23

    very nice article..keep it!

  13. Jeh
    June 15, 2011 at 4:48 am | #24

    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

    • June 15, 2011 at 5:00 pm | #25

      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

  14. seetha
    August 26, 2011 at 5:49 am | #26

    Error 3 Closing tag for element ” was not found. D:\dotnet\wcf\SilverSlideShow\SilverSlideShow\MainPage.xaml 33 5 SilverSlideShow

  15. seetha
    August 26, 2011 at 5:50 am | #27

    Error 5 The attachable property ‘ItemsPanel’ was not found in type ‘ListBox’. D:\dotnet\wcf\SilverSlideShow\SilverSlideShow\MainPage.xaml 19 14 SilverSlideShow

  16. November 28, 2011 at 2:11 pm | #28

    sir i want bind selected item in listbox (images item) so plz me replay ..

  17. January 2, 2012 at 5:09 pm | #29

    Nice Article. I have implemented it. Thanks for Sharing.

  1. August 29, 2010 at 3:33 am | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 34 other followers