Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Silverlight » TabControl in Silverlight

TabControl in Silverlight

This article discusses how to create and use a TabControl available in Silverlight 2.0.

Author Rank:
Technologies: .NET 3.0 and 3.5, Silverlight, XAML,Visual C# .NET
Total downloads : 243
Total page views :  14980
Rating :
 4/5
This article has been rated :  1 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
SilverlightTabControl.zip
 
ArticleAd
Become a Sponsor




This article discusses how to create and use the TabControl available in Silverlight 2.0.

Creating a Silverlight Application

Create a Silverlight application using Visual Studio 2008 by selecting New Project >> Silverlight Application from the Templates list. See Figure 1.


Figure 1. Creating a Silverlight Application.

After the application is created, you land on Page.xaml page in the designer. The XAML of Page.xaml looks like following:

<UserControl

    x:Class="MyFirstSilverlightApplication.Page"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Width="800" Height="600">

    <Grid x:Name="LayoutRoot" VerticalAlignment="Top" Background="White">

       

 

    </Grid>

</UserControl>

 

In the left side bar, you will see a Toolbox that lists Silverlight controls available to you. Now simply drag a TabControl from the Toolbox to your XAML file inside the Grid tag. This action will add System.Controls.Extended namespace reference to your XAML file that looks like this:

 

xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Extended"

 

You will also see the following tag is added for the tab control.

 

<my:TabControl />

 

Now, similar to any other XAML controls, you can set tab control properties using XAML attributes. For example, the following code sets the height, font, font size, and background color of the tab control.

 

<my:TabControl Height="150" FontFamily="Verdana" FontSize="12" Background="Orange">

           

</my:TabControl>

 

You may also add some properties outside of the tab control. For example, the following code sets the background property of the tab control using TabControl.Background.

 

  <my:TabControl Height="150" FontFamily="Verdana" FontSize="12" >

            <my:TabControl.Background>

                <SolidColorBrush Color="Green" Opacity="0.30"/>

            </my:TabControl.Background>

  </my:TabControl>

 

Adding Tab Items

You will hardly recognize a tab control without tab items. The <TabItem \> tag represents a tab item. The Header property of TabItem represents the header text of the header.  The following code creates a tab item with header text "Circle".

<my:TabItem Header="Circle">              

</my:TabItem>

 

A tab item can host other XAML controls similar to a panel or grid control. For example, the following code adds a StackPanel to the tab item and on that StackPanel, it creates a circle with height and width 100.

<my:TabItem Header="Circle">

   <StackPanel>

<Ellipse Height="100" Width="100" StrokeThickness="5" Stroke="black"

      Fill="gold"/>

   </StackPanel>

</my:TabItem>

 

Using the same above approach, I add three tab items to the tab control called Circle, Rectangle, and Polygon. See the code listed in Listing 1.

<my:TabControl Height="150" FontFamily="Verdana" FontSize="12" >

            <my:TabControl.Background>

                <SolidColorBrush Color="Green" Opacity="0.30"/>

            </my:TabControl.Background>

            <my:TabItem Header="Circle">

                <StackPanel>

                    <Ellipse Height="100" Width="100" StrokeThickness="5" Stroke="black"

      Fill="gold"/>

                </StackPanel>

            </my:TabItem>

            <my:TabItem Header="Rectangle">

                <StackPanel>

                    <Rectangle Fill="Yellow" Width="100" Height="100" Stroke="Blue" StrokeThickness="5">

                    </Rectangle>

                </StackPanel>

            </my:TabItem>

            <my:TabItem Header="Polygon">

                <StackPanel>

                    <Polygon Points="100,50 50,100 150,100 100,50 100,30"

       Stroke="green" StrokeThickness="3" Fill="Yellow"/>

                </StackPanel>

            </my:TabItem>

</my:TabControl>

Listing 1. Adding three tab items to a tab control.

If I run the application, the Circle tab shows me a circle looks like Figure 2.

Figure 2.

If I click on Rectangle and Polygon, the output looks like Figure 3 and Figure 4 respectively.

 

Figure 3.

 

Figure 4.

Formatting Tab Items

Let's get some creative. We will display the images on the tab items as the part of tab item headers. The final tab control would look like Figure 5.

 


Figure 5. Tab item header with drawings

To achieve this, I separated tab item header and put a stack panel within it. The stack panel then hosts a text and a drawing depending on what tab item it is.

 

<my:TabItem.Header>

    <StackPanel Orientation="Horizontal">

     <Polygon Points="20,10 20,10 15,15 20,10 20,15"                         Stroke="green" StrokeThickness="3" Fill="Yellow"/>

     <TextBlock Text="Polygon" Margin="1,1,1,1" VerticalAlignment="Center" />   

    </StackPanel>

</my:TabItem.Header>

Note: If you would like to load images at the top of text of the header, simply remove Orientation="Horizontal" from the stack panel in the above code. The default orientation is vertical.

Using the same approach, you may load an image in the header of a tab item. Simply, replace tab header code above that is creating a polygon with the following code. Make sure your image is available in your project.

<Image Height="20" Source="MyImage.jpg" />

Summary

In this article, we saw how to create and use a TabControl in a Silverlight application. We also saw, how to make a tab item header and its contents more interactive by simply adding a few lines of code to our XAML file.  


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Mahesh Chand
Mahesh is a software consultant, architect, author, MCP, MVP, and founder of C# Corner. He has over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries including Microsoft, Unisys, Barclay’s, Centocor (J&J), McGraw-Hill, Excelon, PMI, and University of Pennsylvania Hospital. Since year 2000, he is been working with, ASP.NET, SQL Server, C# and .NET. His latest experience and interest is Silverlight, WPF, WCF, XAML and .NET 3.5. If you need any consulting in ASP.NET, AJAX, WPF, WCF, or XAML, contact him at mahesh AT c-sharpcorner DOT com
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.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
SilverlightTabControl.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
Tab ControlSyed Habeeb Ull6/12/2009

Thanks for the support on SilverLight.

I would be thankful to you if you provide me good tab control within ASP.NET and C#.

My email is shuq@rediffmail.com

Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved