Menus, part III : animated multi-level drop-down menu

by gleblanc 2. November 2008 20:22
In this post, we will add (dynamically and by program) an animation to our previous drop-down and XML-driven menu. Just of few lines to add !


Project / source code

The artwork initially presented is "La montagne Sainte-Victoire" from Paul Cézanne.

As seen in the previous post, our sub-menus and menu items are dynamically created when needed. When a sub-menu is created, we just need to dynamically add a Storyboard object. The code here is added in our CreateSubMenu fonction, that accepts mnu, of type Menu, as argument. In our Menu class, we added a field, named stbShow and of type Storyboard :

 if (mnu.parentMenu != null)  // not applicable to the main (horizontal) menu
 {
  // dynamically create storyboard
  Storyboard stb = new Storyboard();
  Storyboard.SetTarget(stb, mnu.menuContainer);

  DoubleAnimation daHeight = new DoubleAnimation();
  daHeight.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 400));
  daHeight.From = 0; daHeight.To = mnu.lstMenuItems.Count * cellItemHeight;
  Storyboard.SetTargetProperty(daHeight, new PropertyPath("Height"));

  DoubleAnimation daWidth = new DoubleAnimation();
  daWidth.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 400));
  daWidth.From = 0; daWidth.To = mnu.Width;
  Storyboard.SetTargetProperty(daWidth, new PropertyPath("Width"));

  stb.Children.Add(daHeight); stb.Children.Add(daWidth); 
  mnu.menuContainer.Resources.Add("stbShowHide", stb);
  mnu.stbShow = stb;
 }
 .....
 mnu.stbShow.Begin();

Just a few explanations : our menu is a Menu object (Menu being defined in our program). It's implemented as a Border container containing a vertical StackPanel. A reference to this Border container is kept in the menuContainer field of our Menu class. We define two DoubleAnimation to increase Height and Width properties from 0 to their final values in 400 millisec. Final Height depends on number of menu items (value given by the lstMenuItems list of menu items) and final Width depends on largest width for menu items (value kept in the Width field). The two DoubleAnimation are added as children of the Storyboard object just created. The Storyboard is then added as resource of the menu container (ie the Border container).

That's it ! 

Tags:

Comments

11/3/2008 7:28:36 PM #

Pingback from mgalinks.wordpress.com

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

mgalinks.wordpress.com

11/4/2008 3:36:12 AM #

Trackback from Community Blogs

Silverlight Cream for November 03, 2008 -- #418

Community Blogs

11/12/2008 4:50:42 PM #

Pingback from ebnerj.at

Menüs in Silverlight

ebnerj.at

11/16/2008 8:07:15 PM #

Thanks for all 4 parts.

Silverlight Travel Switzerland

6/19/2009 12:45:39 PM #

  
    Hello Gleblanc,

        Thank you.  

anuradha India

7/11/2009 12:27:23 PM #

Hi Hello Leblanc,

Great work... How can we make it in such a way on page load all the submenus for all the menus populate down with the same animation as it is seen when we mouseover each menu.. When i tried editing your code it is working but the stackpanels for submenus are getting positioned to the very first menu item.. That means I am getting all submenus for all items on top of the submenus for the first item.. can I have a support to solve this ???

Vimal saifudin U.A.E.

8/4/2009 4:57:25 AM #

Please, can you PM me and tell me few more thinks about this, I am really fan of your blog...gets solved properly asap.

payday loans United States

8/4/2009 4:57:29 AM #

It sounds like you're creating problems yourself by trying to solve this issue instead of looking at why their is a problem in the first place.

payday loans United States

8/9/2009 12:44:11 AM #

Thanks alot for all 4 parts

Refrigerator water filters United States

8/10/2009 1:18:52 PM #

Hi,

Nice article.I was searching this information ....Thank you so much...

online poker United States

8/10/2009 1:22:33 PM #

Hi,

Good post.....Valuable information for all.I will recommend my friends to read this for sure…

Virus software United States

8/12/2009 12:05:13 PM #

Hi,

Nice article......I really like this source code presentation.....thank you for sharing with us....

Antivirus firewall United States

8/14/2009 9:48:21 AM #

I hadn�t thought of it before, but it�s definitely an interesting idea. Thanks for the insight.

Medicare Advantage Plans United States

8/17/2009 12:17:33 PM #

What is the difference in animation 2d and 3d?

SEO Reseller United States

8/20/2009 3:05:51 AM #

Interesting topic. I wished I could read more, but i have to go back to work now... But I'll be back.

Learn German United States

8/24/2009 2:34:28 PM #

How to install these master pages of the silver light. Need the instructions for the new visitors.

tetris Syria

8/25/2009 12:32:49 PM #


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

Web design United Kingdom

8/28/2009 6:45:05 AM #

Did anyone ever answer Vimal (above)

I have pretty much the same question

(quoted so you dont have to scroll) Great work... How can we make it in such a way on page load all the submenus for all the menus populate down with the same animation as it is seen when we mouseover each menu.. When i tried editing your code it is working but the stackpanels for submenus are getting positioned to the very first menu item.. That means I am getting all submenus for all items on top of the submenus for the first item.. can I have a support to solve this ???

great blog btw.

Thanks,

Derek

payday loans United Kingdom

8/29/2009 10:01:51 PM #

This information has been very helpful to me

web

8/30/2009 1:07:22 PM #

I don�t usually reply to posts but I will in this case.  Smile

bad credit loans United States

8/30/2009 1:07:27 PM #

amazing stuff thanx Smile

bad credit loans United States

9/4/2009 5:17:35 PM #

Very Interesting article will make sure I check your posts more often!

online casinos United States

9/10/2009 11:06:45 AM #


Hmm strange this post is totaly irrelevant to the search query I entered in google but it was listed on the first page.

Internet marketing United Kingdom

9/10/2009 1:08:57 PM #

This is very relavent to my search. Thanks for sharing your views on the topic. It makes one think and look the other side of the story.

free poker United States

9/10/2009 5:16:50 PM #

Thanks for the post

Samantha Peppercorn United Kingdom

9/15/2009 10:52:20 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 10:34:38 PM #

How to install these master pages of the silver light. Need the instructions for the new visitors.

Emo

Emo United States

9/18/2009 1:09:10 PM #

How to use windows silver light and there is no icon that shows that it is installed please help?

cheap laptop United States

9/19/2009 11:15:00 AM #

What is 'Silverlight'? I have downloaded it from MSN, but don't know what are its usage and how it is used.?

Life insurance United States

9/20/2009 6:25:54 PM #

Great coding. I cannot stop admiring your blog.

lazer hair removal los angeles United States

9/21/2009 1:17:38 PM #

Nice blog, dude. I have just bookmarked it.

website development United Kingdom

9/21/2009 1:21:26 PM #

I have never been so excited after reading your post. Thanks for those nice feelings.

перевод с английского United Kingdom

9/22/2009 12:04:11 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/29/2009 2:36:01 PM #

Useful blog, i will come in frequently.

ngan hang Vietnam

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

Thanks for all 4 parts.

Tiffany Rings People's Republic of China

9/30/2009 11:34:54 AM #


hello, this is my first time i visit here. I found so many interesting in your blog especially on how to determine the topic. keep up the good work.

Travel Insurance Online United Kingdom

9/30/2009 3:51:11 PM #

Please, can you PM me and tell me few more thinks about this, I am really fan of your blog...gets solved properly asap.

sloto cash review United States

9/30/2009 5:35:07 PM #

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

laptops United States

10/2/2009 6:36:30 AM #

Wow and wow! Thank you! I always wished to write in my site something like that. Can copy a part of your post to my blog?

payday loans United States

10/2/2009 6:36:36 AM #

That�s Too nice, when it comes in india hope it can make a Rocking place for youngster.. hope that come true.

payday loans United States

10/2/2009 4:15:51 PM #

my God, i thought you were going to chip in with some critical insight at the end there, not leave it with �we leave it to you to decide�.

personal loan United States

10/4/2009 4:49:27 AM #

Get the latest news for online casino gambling

online casinos news United States

10/7/2009 1:23:23 AM #

We are a collection of volunteers and opening a new plan in a society. Your blog afford us priceless information to work on. You have done a marvelous job!

search engine optimisation United Kingdom

10/7/2009 4:24:18 AM #

Very nicely done script. It will be useful in my upcoming projects.

SEO Australia

10/7/2009 6:05:49 PM #

Microsoft Silverlight sound like a program I would be interested to install when I can find the time to do it I like your blog it was interested to read good post keep it up ok.

rn to bsn United States

10/8/2009 12:46:06 PM #

What is the code behind the water filter system?Why its varied from one to another company?

countertop water filter United States

10/10/2009 1:16:00 AM #

You made some good points there. I did a search on the topic and found most people will agree with your blog.

cash loans United States

10/11/2009 8:19:32 AM #

Thank you very this news was very useful for me!

alternative medicine Spain

10/11/2009 10:07:48 AM #

Nice thinking Smile

cash advance United States

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