Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
4.89/5 (58 votes)
2 Aug 2009Ms-PL4 min read 703.5K   6.1K   131   190
This article describes how to create and use the Silverlight Carousel control.
screen capture

Introduction and Background

What is Carousel? Carousel is just a Silverlight 2.0 (now it's has been updated to Silverlight v3) 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 (v3 also) 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.
    XML
    <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.
    XML
    <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>

The ImageSource property just is the Silverlight Image's source, so you can set it any valid Url as Image's source do, more info about it you can refer MSDN's Image's source property.

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.

C#
// 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.

C#
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.
  • 2nd August,2009, Updated the control to Silverlight V3.

My English is not very good so I hope you can understand what I've said. The sample code  include some pictures from public web, you should not use the image in your product.

License

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


Written By
Team Leader
China China
The God created the world.
The programer made the world easy.
I am a programer like c# and c++, so I am writting application with 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."

Comments and Discussions

 
AnswerRe: Can not run the Carousel in WinXP , in VISTA it runs Pin
cokkiy13-Dec-08 19:26
cokkiy13-Dec-08 19:26 
GeneralRe: Can not run the Carousel in WinXP , in VISTA it runs Pin
ruedith16-Dec-08 6:33
ruedith16-Dec-08 6:33 
GeneralRe: Can not run the Carousel in WinXP , in VISTA it runs Pin
cokkiy18-Dec-08 2:45
cokkiy18-Dec-08 2:45 
GeneralRe: Can not run the Carousel in WinXP , in VISTA it runs Pin
ruedith18-Dec-08 6:01
ruedith18-Dec-08 6:01 
GeneralCockiyes carousel embedded in an ASPX-site Pin
ruedith11-Dec-08 6:55
ruedith11-Dec-08 6:55 
AnswerRe: Cockiyes carousel embedded in an ASPX-site Pin
cokkiy12-Dec-08 0:10
cokkiy12-Dec-08 0:10 
AnswerRe: Cockiyes carousel embedded in an ASPX-site Pin
ruedith15-Dec-08 9:58
ruedith15-Dec-08 9:58 
AnswerRe: Cockiyes carousel embedded in an ASPX-site Pin
cokkiy16-Dec-08 0:40
cokkiy16-Dec-08 0:40 
First of all, the Carousel just is a Silverlight control, so if you want to use it, you must have a Silverlight application. Your webpage only can host a Silverlight app instead of a Silverlight control ( a control can only used in a SL app).
Second, Silverlight is a Client Side technology, it's not running on server side, so you can not add a SL project or assembly reference to your aspx-Website(it's a server side tech). The silverlight app very like a flash movie. When you compile the SL app, it's creating a .xap file. In your web page you all need is the .xap file (just like a flash .swf file) and resource files like images and embeded it in your web page (aspx file or html file) just like you embeded a flash moive in your web page file.

The God created the world.
The programer made the world easy.

Generalmouse over event Pin
MikeMercado10-Dec-08 8:58
MikeMercado10-Dec-08 8:58 
QuestionRe: mouse over event Pin
cokkiy11-Dec-08 0:36
cokkiy11-Dec-08 0:36 
QuestionRe: mouse over event Pin
MikeMercado11-Dec-08 4:25
MikeMercado11-Dec-08 4:25 
QuestionRe: mouse over event Pin
cokkiy11-Dec-08 5:05
cokkiy11-Dec-08 5:05 
AnswerRe: mouse over event Pin
ruedith19-Jan-09 5:29
ruedith19-Jan-09 5:29 
Questioncarousel items Pin
MikeMercado10-Dec-08 8:09
MikeMercado10-Dec-08 8:09 
AnswerRe: carousel items Pin
cokkiy11-Dec-08 1:55
cokkiy11-Dec-08 1:55 
Generalcompiling carousel-project and copy the files ti IIS-Webaserver (for localhost) Pin
ruedith9-Dec-08 11:18
ruedith9-Dec-08 11:18 
AnswerRe: compiling carousel-project and copy the files ti IIS-Webaserver (for localhost) Pin
cokkiy10-Dec-08 1:29
cokkiy10-Dec-08 1:29 
GeneralRe: compiling carousel-project and copy the files ti IIS-Webaserver (for localhost) Pin
Bee Nix5-Aug-09 16:35
Bee Nix5-Aug-09 16:35 
GeneralRe: compiling carousel-project and copy the files ti IIS-Webaserver (for localhost) Pin
cokkiy5-Aug-09 23:56
cokkiy5-Aug-09 23:56 
GeneralRe: compiling carousel-project and copy the files ti IIS-Webaserver (for localhost) Pin
Bee Nix6-Aug-09 17:07
Bee Nix6-Aug-09 17:07 
Questionnice Pin
godspeed_yjx4-Dec-08 2:28
godspeed_yjx4-Dec-08 2:28 
AnswerRe: nice Pin
cokkiy4-Dec-08 4:15
cokkiy4-Dec-08 4:15 
QuestionWeb page carousel? Pin
PiggyJ2-Dec-08 18:38
PiggyJ2-Dec-08 18:38 
AnswerRe: Web page carousel? Pin
cokkiy2-Dec-08 23:56
cokkiy2-Dec-08 23:56 
GeneralCarousel is not running in VS Team Suite 2008 Pin
ruedith2-Dec-08 4:17
ruedith2-Dec-08 4:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.