Silverlight Slider

On http://www.microsoft.com/en/us/default.aspx you will get a small Silverlight component with slider effect. Today we will discuss that how can we develop it. This is a stackPanel based component. As you know Silverlight have three main container type controls canvas, grid and stackpanel. Stackpanel have a property that its immediate children never overlap with eachother.
Concept.:
To create a Slider we can use StackPanel as parent and other 6 elements as children.
Stackpanel
| Panel 1 |
Container1 |
Panel2 |
Container2 |
Panel3 |
Containter3 |
|
Now hide all container and attach Mouse Enter event with panels. On Mouse Enter to panel it’s container should visible and width should be increased and when mouse go over other panel this container should shrink and other relative container with should be increase. To do this we can take help of story board.

Coding.
Xaml ..
<UserControl x:Class=”Slider.Page”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Width=”800″ Height=”500″ xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″ xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″ mc:Ignorable=”d”>
<UserControl.Resources>
<Storyboard x:Name=”sbPnl1Show”>
<DoubleAnimationUsingKeyFrames BeginTime=”00:00:00″ Storyboard.TargetName=”gridPanel1Container” Storyboard.TargetProperty=”(FrameworkElement.Width)”>
<SplineDoubleKeyFrame KeyTime=”00:00:01″ Value=”300″/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Name=”sbPnl1Hide”>
<DoubleAnimationUsingKeyFrames BeginTime=”00:00:00″ Storyboard.TargetName=”gridPanel1Container” Storyboard.TargetProperty=”(FrameworkElement.Width)”>
<SplineDoubleKeyFrame KeyTime=”00:00:01″ Value=”0″/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Name=”sbPnl2Show”>
<DoubleAnimationUsingKeyFrames BeginTime=”00:00:00″ Storyboard.TargetName=”gridPanel2Container” Storyboard.TargetProperty=”(FrameworkElement.Width)”>
<SplineDoubleKeyFrame KeyTime=”00:00:01″ Value=”300″/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Name=”sbPnl2Hide”>
<DoubleAnimationUsingKeyFrames BeginTime=”00:00:00″ Storyboard.TargetName=”gridPanel2Container” Storyboard.TargetProperty=”(FrameworkElement.Width)”>
<SplineDoubleKeyFrame KeyTime=”00:00:01″ Value=”0″/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Name=”sbPnl3Show”>
<DoubleAnimationUsingKeyFrames BeginTime=”00:00:00″ Storyboard.TargetName=”gridPanel1Container3″ Storyboard.TargetProperty=”(FrameworkElement.Width)”>
<SplineDoubleKeyFrame KeyTime=”00:00:01″ Value=”300″/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Name=”sbPnl3Hide”>
<DoubleAnimationUsingKeyFrames BeginTime=”00:00:00″ Storyboard.TargetName=”gridPanel1Container3″ Storyboard.TargetProperty=”(FrameworkElement.Width)”>
<SplineDoubleKeyFrame KeyTime=”00:00:01″ Value=”0″/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<Grid x:Name=”LayoutRoot” Background=”White”>
<StackPanel Height=”400″ Margin=”0,0,0,0″ Width=”600″ Orientation=”Horizontal”>
<Canvas Width=”100″ x:Name=”canPanel1″ MouseEnter=”canPanel1_MouseHover”>
<Rectangle Height=”400″ Width=”100″ Fill=”#FFFBAEAE” Stroke=”#FF000000″/>
<TextBlock Height=”62″ Width=”71″ Canvas.Left=”12″ Canvas.Top=”16″ Text=”Panel1″ TextWrapping=”Wrap”/>
</Canvas>
<Grid Width=”0″ x:Name=”gridPanel1Container” RenderTransformOrigin=”0.5,0.5″>
<Rectangle Height=”400″ Width=”300″ Fill=”#FFFFC5C5″ Stroke=”#FF000000″/>
<TextBlock Height=”16″ Width=”Auto” Text=”Container1″ TextWrapping=”Wrap” Canvas.Left=”12″ Canvas.Top=”16″/>
</Grid>
<Canvas Width=”100″ x:Name=”canPanel2″ MouseEnter=”canPanel2_MouseHover”>
<Rectangle Height=”400″ Width=”100″ Fill=”#FF475E91″ Stroke=”#FF000000″/>
<TextBlock Height=”16″ Width=”Auto” Text=”Panel2″ TextWrapping=”Wrap” Canvas.Left=”12″ Canvas.Top=”16″/>
</Canvas>
<Grid Width=”0″ x:Name=”gridPanel2Container”>
<Rectangle Height=”400″ Width=”300″ Fill=”#FFADBCDE” Stroke=”#FF000000″/>
<TextBlock Height=”16″ Width=”Auto” Text=”Cantainer2″ TextWrapping=”Wrap” Canvas.Left=”12″ Canvas.Top=”16″/>
</Grid>
<Canvas Width=”100″ x:Name=”canPanel3″ MouseEnter=”canPanel3_MouseHover”>
<Rectangle Height=”400″ Width=”100″ Fill=”#FFA53CA3″ Stroke=”#FF000000″/>
<TextBlock Height=”16″ Width=”Auto” Text=”Panel3″ TextWrapping=”Wrap” Canvas.Left=”12″ Canvas.Top=”16″/>
</Canvas>
<Grid Height=”Auto” Width=”0″ x:Name=”gridPanel1Container3″ HorizontalAlignment=”Stretch”>
<Rectangle Height=”400″ Width=”300″ Fill=”#FFFDC3FC” Stroke=”#FF000000″ HorizontalAlignment=”Stretch”/>
<TextBlock Height=”16″ Width=”Auto” Text=”Container3″ TextWrapping=”Wrap” Canvas.Left=”12″ Canvas.Top=”16″/>
</Grid>
</StackPanel>
</Grid>
</UserControl>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Slider
{
public partial class Page : UserControl
{
private int selectedPanel;
private enum canPanel
{
panel1,
panel2,
panel3
} ;
public Page()
{
InitializeComponent();
sbPnl1Show.Begin();
selectedPanel = (int)canPanel.panel1;
}
private void canPanel1_MouseHover(object sender, MouseEventArgs e)
{
if(selectedPanel == (int)canPanel.panel2)
{
sbPnl2Hide.Begin();
}
if(selectedPanel == (int)canPanel.panel3)
{
sbPnl3Hide.Begin();
}
sbPnl1Show.Begin();
selectedPanel = (int) canPanel.panel1;
}
private void canPanel2_MouseHover(object sender, MouseEventArgs e)
{
if (selectedPanel == (int)canPanel.panel1)
{
sbPnl1Hide.Begin();
}
if (selectedPanel == (int)canPanel.panel3)
{
sbPnl3Hide.Begin();
}
sbPnl2Show.Begin();
selectedPanel = (int)canPanel.panel2;
}
private void canPanel3_MouseHover(object sender, MouseEventArgs e)
{
if (selectedPanel == (int)canPanel.panel1)
{
sbPnl1Hide.Begin();
}
if (selectedPanel == (int)canPanel.panel2)
{
sbPnl2Hide.Begin();
}
sbPnl3Show.Begin();
selectedPanel = (int)canPanel.panel3;
}
}
}
after that you can put anything in containers.