Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » Silverlight » Slider Control in Silverlight

Slider Control in Silverlight

This step by step article shows how to use the Slider control available in Silverlight 2.0.

Author Rank :
Page Views : 4281
Downloads : 35
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
SliderSample.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Slider Control

The Slider element in XAML represents a Silverlight Slider control.  

 

<Slider></Slider>

 

Slider Control Properties

The Width and Height property represent the width and the height of the control.  The x:Name property represents name of the control, which is a unique identifier of the control.  The Background property is used to set the background color of the control. The Minimum and Maximum properties represent the minimum and maximum values of the slider range.

 

The code snippet in Listing 1 creates a Slider control and sets its name, height, width, background, maximum, and minimum properties. .

 

<Slider x:Name="mcSlider" Width="300" Height="20"

        Background="Gray" Maximum="100" Minimum="0">           

</Slider>

Listing 1

 

The code snippet in Listing 1 generates output looks like Figure 1.

Figure 1

The IsFocused property indicates whether the slider control has focus and IsDirectionReversed property represents the direction of increasing value. By default, the slider control sits on UI in horizontal direction. By using the Orientation property, a slider control can be placed vertically.

 

The code snippet in Listing 2 sets the orientation of a slider control vertical.

 

<Slider x:Name="mcSlider" Width="300" Height="20"

        Background="Gray" Maximum="100" Minimum="0">           

</Slider>

Listing 1

 

The code snippet in Listing 2 generates output looks like Figure 2.

Figure 2

The Application

Now we are going to create a real world application that will use Slider controls values to create a color from three values Red, Green, and Blue respectively. From these three values, we will create a color and fill an ellipse with that color.

First we create a UI page with one circle and three slider controls that looks like Figure 3.

Figure 3

The code that generates Figure 3 is listed in Listing 2.

<Canvas x:Name="LayoutRoot" Background="LightGray" >

    <!-- Create an Ellipse -->

    <Ellipse x:Name="mcCircle" Width="200" Height="200"               

            Canvas.Left="60" Canvas.Top="20"

             Fill="Gray" Stroke="Black" StrokeThickness="2">           

    </Ellipse>

   

    <!-- Create Slider controls -->

    <Slider x:Name="RedSlider" Width="300" Height="20"

            Background="Red" Maximum="255" Minimum="0"              

            Canvas.Left="30" Canvas.Top="240"

            ValueChanged="RedSlider_ValueChanged"/>

    <Slider x:Name="GreenSlider" Width="300" Height="20"

            Background="Green" Maximum="255" Minimum="0"                

            Canvas.Left="30" Canvas.Top="270"

            ValueChanged="GreenSlider_ValueChanged"/>

    <Slider x:Name="BlueSlider" Width="300" Height="20"

            Background="Blue" Maximum="255" Minimum="0"               

            Canvas.Left="30" Canvas.Top="300"

            ValueChanged="BlueSlider_ValueChanged"/>

</Canvas>

Listing 2 

Now, on the ValueChanged event of slider controls, we write the code listed in Listing 3. In this code, we simply create a Color from the values of the slider controls and create a brush with this color and fill the circle.

 

private void RedSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)

{

    UpdateCircleWithColors();

}

 

private void GreenSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)

{

    UpdateCircleWithColors();

}

 

private void BlueSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)

{

    UpdateCircleWithColors();

}

 

private void UpdateCircleWithColors()

{

    Color clr = Color.FromArgb(255, Convert.ToByte(RedSlider.Value),

        Convert.ToByte(GreenSlider.Value), Convert.ToByte(BlueSlider.Value));

    mcCircle.Fill = new SolidColorBrush(clr);

}

 

Listing 3

Now if you run the application and change slider values, you will see the fill color of circle changes accordingly. See Figure 4.

Figure 4

Summary

In this article, I discussed how to create and use a Slider control available in Silverlight 2.0.  We also saw, how to create a real world application using slider controls.

 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dinesh Beniwal
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
important by Marcus On December 7, 2010
hello there, I really like this blog because is a slightly atypical blog, since most of them are about nature workshops, and also I enjoy to read something about it.
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.