Menus, part V : Sliding menu

by gleblanc 25. November 2008 02:06

We continue our serie of animated menus, this time : the sliding menu.


Project / source code

As usual, our sliding menu is declared in a XML file, Menu.xml, inserted as a resource :
  <?xml version="1.0" encoding="utf-8" ?>
 <Menu>
  <Item Text="Italy" >
   <Item Miniature="MiniVenice.jpg" Id="11" Text="Venice, Grand Canal"/>
   .....
   <Item Miniature="MiniVatican.jpg" Id="15" Text="Vatican, St. Peter's Square" />
  </Item>
  <Item Text="USA" >
   <Item Miniature="MiniYosemite.jpg" Id="21" Text="Yosemite National park" />
   .....
   <Item Miniature="MiniGrand Canyon.jpg" Id="23" Text="Grand Canyon National Park"/>
  </Item>
  .....
 </Menu>

The user has just one constraint : reserve space for the menu on top of anything else in the window (this explains the Canvas.ZIndex) . Miniatures must be included as resources (they are displayed in sub-menus). The user can then handle the Click event :
<Grid x:Name="LayoutRoot" Background="White" >
  <Grid x:Name="menuGrid" Canvas.ZIndex="99" Height="40" VerticalAlignment="Top" >
   <gl:Menu Click="menuGrid_Click" />
  </Grid>
  <Grid x:Name="mainGrid" >
   .....
  </Grid>
 </Grid>

As we did for other menu, we build a Silverlight user control named Menu.xaml in the glMenu namespace. Our menu as a Grid containing a Canvas for the sub-menus and an horizontal StackPanel for the main menu items. Why a Canvas for the sub-menus ? They are animated (down animation for opening and up animation for closing) and it is not (unfortunately) possible to animate the Margin property :
<Grid x:Name="MenuRoot" >
  .....
  <Canvas x:Name="canSubMenus" />
  <StackPanel x:Name="spMainMenu" Orientation="Horizontal" Loaded="spMainMenu_Loaded" >
   ....
  </StackPanel>
 </Grid>

Since main menu items and sub-menu items are quite different, we create two classes : MainMenuItem and SubMenuItem. In memory, we also keep a list of main menu items and a table for sub-menu items (the number of entries in this table depends on the number of main menu items) :
 List<MainMenuItem> lstMainMenuItems;
 List<SubMenuItem>[] tabSubMenuItems;

The main menu is first build in the spMainMenu_Loaded function. Thanks to Linq for Xml, we analyze the XML file to find the main menu items :
lstMainMenuItems = new List<MainMenuItem>();
 XElement xmlRoot = XElement.Load("Menu.xml");
 getMainMenuItems(xmlRoot);
 .....
 void getMainMenuItems(XElement xml)
 {
  var lstItems = from p in xml.Elements("Item") select p;
  foreach (var el in lstItems)
   lstMainMenuItems.Add(new MainMenuItem {Text=el.Attribute("Text").Value, xElem=el});
 }

The main menu is then build using Silverlight UI elements : a Grid including a TextBlock (indeed two TextBlock to give a 3D effect to the display). For each main menu item, we handle the MouseEnter event (to make visible the corresponding sub-menu), the MouseLeave (to hide it) but also the MouseMove (leaving the main menu item thru the bottom side must leave the sub-menu as it is).

The sub-menus are then build (at this time, we know the number of sub-menu items). All the sub-menus are placed in a Canvas (canSubMenus). Each sub-menus (three here) are build on a canvas. Each sub-menu item (we have 5 for Italy and 3 for USA) are also based on a canvas containing an image (the miniature). For each of these canvas, we handle the MouseEnter, MouseLeave and MouseLeftButtonDown events.

Source code is included, as usual.

Tags:

Comments

11/25/2008 7:33:34 PM #

Pingback from mgalinks.wordpress.com

2008 November 25 - Links for today « My (almost) Daily Links

mgalinks.wordpress.com

11/27/2008 2:02:42 PM #

Trackback from Community Blogs

Silverlight Cream for November 26, 2008 -- #438

Community Blogs

1/20/2009 8:58:32 AM #

Hellow, nice blog, tnx for the information......nice idea

Busby SEO Test United States

3/12/2009 4:22:14 PM #

im going to started it

Perfect Home Design United States

6/28/2009 8:08:28 AM #

Just what i needed

Prasad

6/28/2009 8:09:01 AM #

just what i needed

Wohlfarth Real Estate

7/14/2009 3:04:44 PM #

Do you accept guest posts? I would love to write couple articles here.

Joe Rodeo United States

7/24/2009 9:26:41 PM #

Do you earn decent money from this blog or are you doing it just for fun?

Jacques Lemans United States

8/5/2009 2:43:19 PM #

Thanks for your information.

SEO United States

8/20/2009 12:48:05 PM #

Can�t believe that I�m the first one to digg this? Anyway, I did. Thanks for this awesome information.

Harley Davidson Motorcycles United States

8/25/2009 12:42:04 PM #

When i go to start and task menu property by right clicking on the taskbar the window disappears soon.why?

Web Design London United States

9/10/2009 9:52:25 AM #

Hi,
  Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work…

Search engine marketing United States

9/14/2009 12:22:26 AM #


I admit, I have not been on this webpage in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues.
Great stuff as usual....

Waterfront Property for sale United States

9/14/2009 7:51:23 PM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

9/15/2009 5:12:34 AM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

9/15/2009 8:59:14 AM #

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.Any way Ill be subscribing to your feed and I hope you post again soon

payday online United States

9/15/2009 6:46:47 PM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

9/16/2009 6:33:07 PM #

Easy option to get useful information as well as share good stuff with good ideas and concepts

payday loans United States

9/16/2009 8:14:21 PM #

I havent any word to appreciate this post.

live soccer scores United States

9/16/2009 8:15:12 PM #

Pretty good post.

scores lives United States

9/16/2009 8:16:13 PM #

Excellent post.I want to thank you for this informative

international soccer scores United States

9/16/2009 8:17:11 PM #

Easy option to get useful information as well as share good stuff with good ideas and concepts

latest scores United States

9/16/2009 8:18:18 PM #

I really appreciate sharing this great post.

football live score United States

9/16/2009 8:19:30 PM #

I admit, I have not been on this webpage in a long time

online cheap Viagra United States

9/16/2009 8:20:15 PM #

Great website

Kamagra Wholesale suppliers United States

9/16/2009 8:20:53 PM #

Hellow, nice blog, tnx for the information......nice idea

kamagra generic viagra United States

9/16/2009 8:21:26 PM #

im going to started it

Buy Kamagra tablets United States

9/16/2009 8:22:05 PM #

Thanks for your information.

UK kamagra wholesale United States

9/21/2009 10:55:57 AM #

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

payday loans United States

9/21/2009 12:05:40 PM #


Admiring the time and effort you put into your blog and detailed information you offer!

Web design United Kingdom

9/21/2009 6:21:58 PM #

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

payday loans United States

9/23/2009 5:27:41 PM #

Hey very nice blog!! Man .. Beautiful .. Amazing .. I will bookmark your blog and take the feeds also...

payday loans United States

9/23/2009 5:27:47 PM #

thanks !! very helpful post!

payday loans United States

9/27/2009 9:28:31 AM #

Great overview. Your style of writing is really a joy to read.

How to Make Money United States

9/28/2009 6:18:37 PM #

Thanks for such lovely ideas as they seem to work in my project.

website design United Kingdom

9/28/2009 6:57:34 PM #

I have checked your post for copies and can tell that there is none like this on the web which makes it worth reading.

Russian translations United States

9/30/2009 11:10:56 AM #

Thanks for such lovely ideas as they seem to work in my project.

italian outdoor fireplaces United Kingdom

9/30/2009 11:33:06 AM #

Just what i needed

Tiffany Rings People's Republic of China

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Gerard Leblanc is the author of several books (in french) on C++, C#, .NET and Silverlight (Eyrolles, Paris as publisher). See www.gleblanc.eu as the companion web site for these books (included sample programs).
He is Microsoft MVP for Silverlight.

MVP logo