Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Silverlight » Creating Photo gallery using AccordionControl in Silverlight 3.0: Part I

Creating Photo gallery using AccordionControl in Silverlight 3.0: Part I

This article is first part of 3 or 4 parts Photo Gallery series using AccordionControl in Silverlight.

Author Rank :
Page Views : 5037
Downloads : 181
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:
AccordianControl.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Objective 

This article will explain 
  1. Basic use of Accordion control in Silverlight 3.0 
  2. Binding Accordion items with properties of entity class. 
  3. Creating an Imagesource from Image URL provided. 
Background 

This article is first part of 3 or 4 parts IPL Photo Gallery series. In this article, I am reading image, team name and caption name from hard coded list. I have added images in the project as existing items. In further article I will modify this to read from database using WCF. 

Expected output 

1.gif

2.gif
   
Follow the steps  

Step 1

Create a Silverlight application. 

Step 2

Create an entity class. This class will contain as property, TeamName and TeamImage of the IPL team.  Team entity class is as below. Just right click on Silverlight project and add a new class named Team. 

namespace AccordianControl
{
    public class Team
    {
        public string TeamName { get; set; }
        public string Capatin { get; set; }
        public ImageSource TeamImage { get; set; }
    }
}

Type of TeamImage is ImageSource because  I am going to bind this property directly to a imagesource. 

Step  3

In this step, I will get an image from the path of the image provided.  I have created function that is returning an ImageSource. 

public ImageSource GetImage(string path)
{
   return new BitmapImage(new Uri(path,UriKind.Relative));
}

Step 4 

Right click and add images in Silverlight project.  To do so; right click and select add existing items.  I have added 8 images corresponding to 8 teams of IPL. 

Step 5

In this step, I am creating a function to return list of Teams. 

public List<Team> GetTeams()
{
    List<Team> lstTeam = new List<Team>()
                         {
                             new Team{TeamName="Rajasthan Royals " , TeamImage = GetImage("RR.jpg"),Capatin="Shane Warne"},
                             new Team{TeamName="Delhi DareDevils " , TeamImage = GetImage("DD.jpg"),Capatin="Gautam Gambhir"},
                             new Team{TeamName="Chenni SuperKing " , TeamImage = GetImage("CS.jpg"),Capatin="Mahendra Singh Dhoni"},
                             new Team{TeamName="Mumbai Indians " , TeamImage = GetImage("MI.jpg"),Capatin="Sachin Tendulkar"},
                             new Team{TeamName="Kolkatta KnightRiders " , TeamImage = GetImage("KK.jpg"),Capatin="Saurabh Ganguli"},
                             new Team{TeamName="Royal Challenger Bangalore " , TeamImage = GetImage("RC.jpg"),Capatin="Anil Kumble"},
                             new Team{TeamName="Deccan Charger Hyderabad " , TeamImage = GetImage("DC.jpg"),Capatin="Adam Ghilchrist"},
                             new Team{TeamName="Kings 11 Punjab" , TeamImage = GetImage("KP.jpg"),Capatin="Kumar Sangakara"}
                         };
    return lstTeam;
}

Step 6

On the page load bind the list of teams as item source to Accordion control. 

public MainPage()
{
    InitializeComponent();
    teamAccordianControl.ItemsSource = GetTeams();
}

Here teamAccordianControl is name of the Accordion control added on XAML page. 

So, complete code for MainPage.Xaml.cs is as below 

MainPage.Xaml.cs

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;
using System.Windows.Media.Imaging;

namespace AccordianControl
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            teamAccordianControl.ItemsSource = GetTeams();
        }
        public ImageSource GetImage(string path)
        {
            return new BitmapImage(new Uri(path,UriKind.Relative));
        }
        public List<Team> GetTeams()
        {
            List<Team> lstTeam = new List<Team>()
                                 {
                                     new Team{TeamName="Rajasthan Royals " , TeamImage = GetImage("RR.jpg"),Capatin="Shane Warne"},
                                     new Team{TeamName="Delhi DareDevils " , TeamImage = GetImage("DD.jpg"),Capatin="Gautam Gambhir"},
                                     new Team{TeamName="Chenni SuperKing " , TeamImage = GetImage("CS.jpg"),Capatin="Mahendra Singh Dhoni"},
                                     new Team{TeamName="Mumbai Indians " , TeamImage = GetImage("MI.jpg"),Capatin="Sachin Tendulkar"},
                                     new Team{TeamName="Kolkatta KnightRiders " , TeamImage = GetImage("KK.jpg"),Capatin="Saurabh Ganguli"},
                                     new Team{TeamName="Royal Challenger Bangalore " , TeamImage = GetImage("RC.jpg"),Capatin="Anil Kumble"},
                                     new Team{TeamName="Deccan Charger Hyderabad " , TeamImage = GetImage("DC.jpg"),Capatin="Adam Ghilchrist"},
                                     new Team{TeamName="Kings 11 Punjab" , TeamImage = GetImage("KP.jpg"),Capatin="Kumar Sangakara"}
                                 };
            return lstTeam;
        }
    }
}

Step 7 

In this step, I will design the page. 
  • Drag and drop a Accordion control from toolbox on page 
  • Set vertical and horizontal alignment of control to stretch. 
  • Set SelectionSequence to CollapsebeforeExpand 
  • Set SelectMode to ZeroOrOne
  • Set the header using Accordion.ItemContainerStyle
  • Inside Data template put a text block and bind to the TeamName property of Team entity class.
  • Accordion.ContentTemplate create item of the Accordion control 
  • Inside Data Template put a Stack panel with orientation vertical. 
  • Put a Text block and bind to Caption property of Team entity class. 
  • Put an Image control and bind to TeamImage property of Team entity class. 
MainPage.Xaml

<UserControl xmlns:layoutToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit"  x:Class="AccordianControl.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="*" d:DesignHeight="*" Background="Azure" >
  <Grid x:Name="LayoutRoot" Height="620" Width="420" >          
        <layoutToolkit:Accordion x:Name="teamAccordianControl"
                                  HorizontalAlignment="Stretch"
                                VerticalAlignment="Stretch"
                                SelectionSequence="CollapseBeforeExpand"
                                Height="auto"
                                SelectionMode="ZeroOrOne">
                <layoutToolkit:Accordion.ItemContainerStyle>
                    <Style x:Name="accordionitemstyle1" TargetType="layoutToolkit:AccordionItem">
                        <Setter Property="HeaderTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <TextBlock Text="{Binding TeamName}" />
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </layoutToolkit:Accordion.ItemContainerStyle>
                <layoutToolkit:Accordion.ContentTemplate >
                <DataTemplate>
                    <StackPanel Height="auto" Width="auto" Orientation="Vertical">
                        <TextBlock HorizontalAlignment="Center" FontFamily="Arial" FontSize="20" Text="{Binding Path=Capatin}" />
                        <Image Source="{Binding Path=TeamImage}"/>
                    </StackPanel>
                </DataTemplate>               
            </layoutToolkit:Accordion.ContentTemplate>
        </layoutToolkit:Accordion>           
  </Grid>
</UserControl>

Now press F5 and output would be 

3.gif

4.gif

Conclusion 

In this article, I have shown basic use of accordion control.  I have shown how to bind Accordion items with different properties of an entity class. I will further modify this sample and add more functionality. Thanks for reading. 

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
 
Dhananjay Kumar
Dhananjay Kumar is a developer who blogs at http://debugmode.net/. He is Microsoft MVP ,Telerik MVP and Mindcracker MVP. You can follow him on twitter  @debug_mode
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 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. 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:
DevExpress Free UI Controls
Become a Sponsor
 Comments
UI Designing for Accordion panels by Vinod On March 25, 2010
I am UI Designer, can i change the accordion panel with my images not +/- collapsible just as menu item of as seen in Microsoft site  has. please help me in this.

thanks
vinod
Reply | Email | Modify 
Re: UI Designing for Accordion panels by Dhananjay On March 28, 2010
Could you explain your question little bit more ? such that I could help you :)
Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.