Click here to Skip to main content
6,293,171 members and growing! (12,370 online)
Email Password   helpLost your password?
Web Development » Silverlight » Controls     Intermediate License: The Microsoft Public License (Ms-PL)

Silverlight Carousel: Creating a Silverlight Control Displays Picture in an Interactive Carousel

By cokkiy

This article describes how to create and use the Silverlight Carousel control.
C# 2.0, C# 3.0.NET 3.0, .NET 3.5, Silverlight, Architect, Dev, Design
Posted:23 Nov 2008
Updated:26 Nov 2008
Views:47,663
Bookmarked:79 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
33 votes for this article.
Popularity: 7.13 Rating: 4.70 out of 5
1 vote, 3.0%
1

2
2 votes, 6.1%
3
3 votes, 9.1%
4
27 votes, 81.8%
5
screen capture

Introduction and Background

What is Carousel? Carousel is just a Silverlight 2.0 control which can display collections of pictures like a carousel (have you ever see a carousel?). When I creating Mashup in Microsoft Popfly I saw a display control named "Carousel." It was very attractive to me so I wanted to find one like this to use it in my own application. Using Google I found YetAnotherCarousel, but it was too coarse to use. And the other one I found was created in Silverlight V1 and was very hard to use. But the idea dogged me, attracted me day and night. I Googled again and again but could not find a perfect one. So one day, I asked myself, "Why not create one for myself?"

Key Features

  • It's really a Silverlight V2 Control, all written in C#.
  • Very simple to use.
  • Has many useful properties to control it's behaviour.
  • Can turning continuous or step by step.

Using the Control

Using the Carousel control is very simple just like any other control. First of all, you add the assembly (cokkiy.display.carousel) and reference it to your Silverlight Project. Then look at the code:

  1. Add the XMLNS reference to your Page XAML file beginning.
    <UserControl x:Class="ControlTest.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:display="clr-namespace:Cokkiy.Display;assembly=Cokkiy.Display.Carousel"
                 Width="800" Height="600">
  2. Place the Carousel in your layout control. I put it in a grid, and you can put it in any UIElement as a content or child item.

    <display:Carousel x:Name="carousel" TurnDirection="Counterclockwise"
          Padding="5,5,5,5" >
                <display:Carousel.Background>
                    <LinearGradientBrush EndPoint="0.5,0" StartPoint="0.5,1">
                        <GradientStop Color="#FF000000"/>
                        <GradientStop Color="#FFFFFFFF" Offset="1"/>
    
                    </LinearGradientBrush>
                </display:Carousel.Background>
                <display:Carousel.ItemSources>
                    <display:ItemSource Title="01" ImageSource="Images/01.jpg"/>
                    <display:ItemSource Title="02" ImageSource="Images/02.jpg"/>
                    <display:ItemSource Title="03" ImageSource="Images/03.jpg"/>
    
                    <display:ItemSource Title="04" ImageSource="Images/04.jpg"/>
                    <display:ItemSource Title="05" ImageSource="Images/05.jpg"/>
                    <display:ItemSource Title="06" ImageSource="Images/06.jpg"/>
                    <display:ItemSource Title="07" ImageSource="Images/07.jpg"/>
                    <display:ItemSource Title="08" ImageSource="Images/08.jpg"/>
                </display:Carousel.ItemSources>
    
            </display:Carousel>

Notice how I added a Carousel in my page. The Carousel has 8 images as its children. Let's look at the screen capture.

Screen capture

How It Works?

From the screen capture we can know the Carousel is composed of three main parts. The Canvas where the image placed, the small item (the carousel leaf) contains an image which can turn around with the ellipse (it is a virtual image ellipse does not exist) and the panel displays the selected image (the large image in the picture). All three parts are also Silverlight controls which have their own properties. The carousel leaf control I called is CarouselItem which calculates the position of where to place the Canvas, and the scale range by itself. The key point is in placing each item in a proper point and making its size fit any position, and making it turn along with the ellipse.

To make the Carousel control like a real carousel, we must ensure:

  • The farther item is smaller than the nearer item, and the nearest item is the biggest item.
  • The item in the back (father) is under the item in the front (near).
  • The item in the foreground is blurred.

Let's see the following figure demo the concept.

carousel figure

When the center point, O, is constant, the positon and size of CarouselItem at point P only depends to the angle of A. We can calc the positon and size like following code.

    // Position 
    double dx = Axis.Width * Math.Cos(newAngle) + Center.X; 
    double dy = Axis.Height * Math.Sin(newAngle) + Center.Y; 
    Canvas.SetLeft(this, dx);
    Canvas.SetTop(this, dy); 
    // Scale 
    double sc = 2 + Math.Cos(newAngle - Math.PI / 2); 
    ScaleTransform st = (
        this.RenderTransform as TransformGroup).Children[0] as ScaleTransform; 
    st.ScaleX = sc; 
    st.ScaleY = sc; 
    // Set the ZIndex based the distance from us, the far item 
    // is under the near item 
    Canvas.SetZIndex(this, (int)dy);

The position and size all only depends on the angle, so it's very easy to make it turn around the ellipse. Silverlight support the DependencyProperty animation, so we make the angle as a DependencyProperty. And make a DoubleAnimation on each item's angle property. The DoubleAnimation has a property named By which means the total amount by which the animation changes its starting value. When we set it to 2*PI, it starts turning a lap.

    Storyboard storyboard = new Storyboard();
    //this.Resources.Add(name, storyboard);

    // Angle animation
    DoubleAnimation doubleAnimation = new DoubleAnimation();
    doubleAnimation.By = 2*Math.PI;
    doubleAnimation.Duration = duration;
    doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;

    // Set storyboard target property
    storyboard.Children.Add(doubleAnimation);
    Storyboard.SetTarget(doubleAnimation, item);
    Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Angle"));

We create a storyboard for each item and then add all these storyboards to a global storyboard. Then if we want the carousel to turn, we just start the global storyboard.

History

  • 23rd November, 2008: Initial post
  • 26rd November, 2008: Fixed a bug in source code. When remove an item from the collection, it's will throw an exception, now fixed.

Notes:

My English is not very good so I hope you can understand what I've said. The sample code does not include the sample pictures, you should add some images to the Images folder of the sample project before running the code. And make sure you have changed the sample page to fix the image name you just added.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

cokkiy


Member
The God created the world.
The programer made the world easy.
I am a programer like c# and c++, so I am writting application in it.
Creating beautiful application make life easy.
If you have a project and looking for a man, I'll be say "hi, I am just the man you are looking for."
Occupation: Chief Technology Officer
Location: China China

Other popular Silverlight articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 123 (Total in Forum: 123) (Refresh)FirstPrevNext
GeneralGot Error at Carousel.CarouselDefault Pinmemberpupplelay2:20 30 Jun '09  
GeneralNot so fast! A bit of a problem! Pinmembercycleguy9:29 24 Jun '09  
GeneralRe: Not so fast! A bit of a problem! Pinmembercokkiy3:36 25 Jun '09  
GeneralGreat carousel! Pinmembercycleguy9:05 24 Jun '09  
GeneralRe: Great carousel! Pinmembercokkiy3:43 25 Jun '09  
GeneralRe: Great carousel! PinmemberASysSolvers7:41 30 Jun '09  
GeneralRe: Great carousel! Pinmembercokkiy2:37 2 Jul '09  
Generalappreciation PinmemberWilliam0658 Su3:50 24 Jun '09  
GeneralWhere can i find Source of TemplateBinding Source for Image Pinmemberpupplelay0:29 19 Jun '09  
GeneralHow to bind images from the database with image source? Pinmemberraghav1921:13 15 Jun '09  
GeneralRe: How to bind images from the database with image source? Pinmembercokkiy4:15 25 Jun '09  
GeneralWhere is cokkiy.display.carousel assembly? Pinmemberraghav195:08 28 May '09  
GeneralRe: Where is cokkiy.display.carousel assembly? Pinmembercokkiy5:25 19 Jun '09  
General"AL.exe" was not found PinmemberMember 29989692:52 18 May '09  
GeneralRe: "AL.exe" was not found Pinmembercokkiy18:49 18 May '09  
GeneralRe: "AL.exe" was not found PinmemberMember 299896919:16 18 May '09  
QuestionDynamically creating Carousel object at run-time. Pinmembersobo12323:12 16 May '09  
AnswerRe: Dynamically creating Carousel object at run-time. Pinmembercokkiy18:53 18 May '09  
GeneralRe: Dynamically creating Carousel object at run-time. Pinmembersobo1230:56 19 May '09  
GeneralDoes anyone know how to modify the HEIGHT of the carousel spinnning??? Pinmembermarkbaer14:18 15 May '09  
GeneralAdding the control to .ASPX page PinmemberMohammad Al Hoss23:57 14 May '09  
GeneralRe: Adding the control to .ASPX page Pinmembercokkiy18:55 18 May '09  
GeneralHow to change image sizes in carousel and in selected image PinmemberPneumas19:08 2 May '09  
QuestionThrow error: No parameterless constructor defined for this object. PinmembersTembhre21:30 29 Apr '09  
QuestionItemSource Name Pinmembernitishadesai17:19 25 Apr '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Nov 2008
Editor: Sean Ewington
Copyright 2008 by cokkiy
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project