5,659,336 members and growing! (20,325 online)
Email Password   helpLost your password?
Web Development » Silverlight » General     Intermediate License: The Code Project Open License (CPOL)

Drop Down menu in Silverlight 2

By RazanPaul

Drop Down menu using popup control in Silverlight 2
C#, .NET (.NET 3.5, .NET), Visual Studio (Visual Studio, VS2008), Dev

Posted: 24 Apr 2008
Updated: 26 May 2008
Views: 15,212
Bookmarked: 13 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 3.87 Rating: 3.03 out of 5
2 votes, 10.5%
1
2 votes, 10.5%
2
2 votes, 10.5%
3
1 vote, 5.3%
4
12 votes, 63.2%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
image1_menu.PNG

Figure 1

image2_menu.PNG

Figure 2

image3_menu.PNG

Figure 3

Download DropDownMenu.zip - 616.32 KB

Introduction

We know that there is no built in control for menu in Silverlight 2.Here a silverlight DropDownMenu is created using silverlight popup control and button.

I'm happy to present my first article on CodeProject.I'm hoping to use the experience to improve my writing style. So your comments, suggestions and criticism are very welcome.

Background

Here firstlevel menuitems are buttons.When a button is clicked then a popupcontrol is opened which contains submenu items.

Using the code

First Here a popup control is declared .When an individual button is clicked then a popup control is opened using the following code.

       
private Popup _popUp;

public Popup PopUp

{

get { return _popUp; }

set { _popUp = value; }

}

 
       private void menuItem_Selected(object sender, MouseButtonEventArgs e)
        {

            if (this.PopUp.Child != null)
            {
                this.PopUp.Child = null;
                this.PopUp.IsOpen = false;
            }

            Button visual = sender as Button;


            GeneralTransform transform = base.TransformToVisual(visual);
            Point point = transform.Transform(new Point(0.0, 0.0));
            StackPanel sp = new StackPanel();


            sp.Children.Clear();
            SubMenu oItem = new SubMenu(_popUp);
            oItem.fillDetails("Setu");


            sp.Children.Add(oItem);
            oItem = new SubMenu(_popUp);
            oItem.fillDetails("Asif");
            sp.Children.Add(oItem);

            oItem = new SubMenu(_popUp);
            oItem.fillDetails(" Know about bangladeshi people ");
            sp.Children.Add(oItem);

            this._popUp.Child = sp;
            _popUp.Child.MouseLeftButtonDown += new MouseButtonEventHandler(Child_MouseLeftButtonDown);

            this._popUp.VerticalOffset = this.Margin.Top + Math.Abs(point.Y) + visual.ActualHeight - 2;
            this._popUp.HorizontalOffset = this.Margin.Left + Math.Abs(point.X);
            this._popUp.IsOpen = true;


        }
 
 

        

Here another issue is communication between Popup control and Silverlight main application Form. The commutation is done using MouseLeftButtonDown.

 _popUp.Child.MouseLeftButtonDown += new MouseButtonEventHandler(Child_MouseLeftButtonDown); 

Another thing is to determine which menuitem is clicked.Here each menuitem is created using a rectangle and textblock wrapped with a GRID.For that the following code is used:

 public event EventHandler menucommand;
        void Child_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.Source is TextBlock)
            {
                string str = ((TextBlock)e.Source).Text;

                if (menucommand != null)
                {
                    menucommand(str, null);
                }

            }
            else if (e.Source is Rectangle)
            {
                string str = string.Empty;
                foreach (FrameworkElement em in ((Grid)((Rectangle)e.Source).Parent).Children)
                {
                    if (em is TextBlock)
                    {
                        str = ((TextBlock)em).Text;
                    }

                }

                if (menucommand != null)
                {
                    menucommand(str, null);
                }

            }

            PopUp.IsOpen = false;
        } 

Reference

Thanks to Jose Fajardo for his blog.

http://advertboy.wordpress.com/2008/04/13/my-digg-mashup-that-taught-me-so-much-about-silverlight-20-beta-part-1/

History

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

RazanPaul


I am pursuing M.Sc. in Computer Science and Engineering from BUET and have been working for an offshore software company in Bangladesh for 3 years. I am experienced in Distributed Application Development,Device programming, Smart Client Development and Web Application Development.

Feel free to mail me for anything at razanpaul AT yahoo Dot com
Occupation: Software Developer (Senior)
Company: Adaptive Enterprise Limited.
Location: Bangladesh Bangladesh

Other popular Silverlight articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
GeneralIt works for SL 2 Beta 2 also!membernasimhuq18:32 9 Aug '08  
GeneralRe: It works for SL 2 Beta 2 also!memberRazanPaul0:04 12 Aug '08  
GeneralNice article but...memberfmonsalvo10:26 11 Jun '08  
GeneralRe: Nice article but...memberRazanPaul22:00 12 Jun '08  
GeneralRe: Nice article but...memberyinpengxiang4:22 14 Jun '08  
General[Message Removed]membernompel3:26 4 Oct '08  
QuestionHow can we know silverlight boundarymembercodejustforfun5:25 12 May '08  
GeneralRefverence?adminChris Maunder5:50 6 May '08  
GeneralCite your referencesmembermcameron4:28 3 May '08  
GeneralRe: Cite your referencesmemberRazanPaul11:06 8 May '08  
GeneralNice articlememberMohammad Ashraful Alam23:06 25 Apr '08  
GeneralGreate ArticlememberMember 280282021:08 24 Apr '08  
GeneralNice Articlemembersetu_raas5:24 24 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 May 2008
Editor:
Copyright 2008 by RazanPaul
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project